/ W3SCHOOLS

W3schools - InnerClass

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Inner Classes

The purpose of nested classes(class within a class)is to group classes that belong together

Which makes your code more readable and maintainable

class OuterClass{
	int x = 10;
	class InnerClass{	// can be private or protected or static
		int y = 5;
		public int myInnerMethod(){
			return x;
		}
	}
}
	// In main method
OuterClass myOuter = new OuterClass();
// to access the inner class, create an object of the outer class
OuterClass.InnerClass myInner = myOuter.new InnerClass();
// and then create an object of the inner class
// if innerclass is static   “OuterClass.InnerClass myInner = new OuterClass.InnerClass();”
	// can access it without creating an object of the outer class
System.out.println(myInner.y + myOuter.x);	// output will be 15;
Systme.out.println(myInner.myInnerMethod());	// output will be 10;
// how to access outer class from inner class