본문 바로가기
Programming/JAVA(eclipse)

java 10 일차 _ 02. 중첩클래스

by yoon9i 2024. 3. 21.

1. 중첩 클래스 ( nested class, inner class )
1) 개념: 클래스내에 또 다른 클래스를 지정할 수 있고
           클래스내에 있는 클래스를 중첩클래스라고 한다.

    형태:
public class 클래스 { // outer 클래스
     static int n = 10; // 가능
     private int n2 = 10;
     int n3 = 20;

                  class 클래스 { // 중첩클래스( inner 클래스 ) 
          static int n2 = 20; // 불가능
          System.out.println(n2 + n3); 
          // private으로 된 변수도 사용 가능
     }
                  static class 클래스 { 
          static int n2 = 20; // 가능
          System.out.println(n2); 
          // outer 클래스의 인스턴스 변수 사용불가
          System.out.println(n3); 
          // outer 클래스의 인스턴스 변수 사용불가
     }
}

2) 특징:
- static 변수 사용 불가. 단, static 으로 된 중첩클래스는 사용가능.
  static 으로 지정된 중첩클래스는 outer 의 인스턴스변수 접근불가.
- 반드시 Outer 생성후 Inner 생성이 가능하다.
==> 다른 클래스가 사용할려면 복잡하다. 따라서 다른 클래스는 사용을
       안하고 Outer 만 일반적으로 사용한다.
- inner 클래스 컴파일된 파일명: Outer클래스명$Inner클래스명.class

3) 종류:
중첩클래스가 outer 클래스내의 어떤 위치에 정의되었냐?

class Outer {
     int n; // 멤버변수
     public void a( ) { // 멤버메서드
           int x = 10; // 로컬변수
           class Inner3 { // Local Inner class

           }
     }

     public Inner( ) { // Member Inner class 
//( 일반적으로 불리우는 중첩클래스 )
     
      }

      static class Inner2 { // Static Inner class

       }
      
      public Outer( ) { }

} // end Outer

* 힙 메모리 상에서 inner 와 outer 는 독립적으로 실행된다.

 

< Member Inner Class >

package exam17_중첩클래스;

// < Member Inner Class >

class Outer {
	
	public int n = 10;
	protected int n2 = 20;
	int n3 = 30;
	private int n4 = 40;
	static int n5 = 50;
	
	public void method() {
		// Outer의 메서드에서 Inner 생성후 사용
		Inner i = new Inner();
		i.innerMethod();

	}
	
	// member Inner class : Outer 클래스의 멤버형태로 존재하기 때문이다.
	class Inner {
		
		int m = 10;
//		static int m2 = 20; // static 변수 선언 불가
		
		public void innerMethod() {
			System.out.println(n);
			System.out.println(n2);
			System.out.println(n3);
			System.out.println(n4); // outer 의 private 도 접근가능.
			System.out.println(n5);
			System.out.println(m);
		}
		
	}// end Inner
	
}// end Outer




public class TestMain3_MemberInnerClass {

	public static void main(String[] args) {
		
		// Outer 생성
		Outer x = new Outer();
		x.method();
		
		System.out.println();
		// Outer 가 아닌 다른 클래스에서 Inner 접근하는 방법
		Outer x2 = new Outer();
		Outer.Inner i = x2.new Inner();
		i.innerMethod();
	}

}

 

< Local Inner Class >

package exam17_중첩클래스;

// < Local Inner Class >

class Outer2 {
	
	public int n = 10;
	protected int n2 = 20;
	int n3 = 30;
	private int n4 = 40;
	static int n5 = 50;
	
	public void method() {
		int x = 100; // 로컬변수
		class Inner { // 로컬변수와 동일한 레벨
			int m = 10; // Inner 클래스 입장에선 인스턴스변수
//			static int m2 = 20; // static 변수 선언 불가
			
			public void innerMethod() {
				System.out.println(n);
				System.out.println(n2);
				System.out.println(n3);
				System.out.println(n4); // outer 의 private 도 접근가능.
				System.out.println(n5);
				System.out.println(m);
			}
			
			
		}// end Inner
		
		// local inner class 는 로컬변수처럼 동작하기 때문에
		// 메서드안에서 사용이 가능하다. 따라서 메서드안에서만 생성할 수 있다.
		Inner i = new Inner();
		i.innerMethod();
		
	}// end method
	
}// end Outer2

public class TestMain4_LocalInnerClass {

	public static void main(String[] args) {
		
		// Outer 생성
		Outer x = new Outer();
		x.method();
	}

}

 

< Static Inner Class >

package exam17_중첩클래스;

// < Static Inner Class>
class Outer3 {
	
	public int n = 10;
	protected int n2 = 20;
	int n3 = 30;
	private int n4 = 40;
	static int n5 = 50;
	
	public void method() {}
	
	// static
	static class Inner {
		
		int m = 10; // Inner 클래스 입장에선 인스턴스변수
		static int m2 = 20; // static 변수선언 가능, 대신 Outer 의 인스턴스 변수 접근불가
		
		public void innerMethod() {
//			System.out.println(n);
//			System.out.println(n2);
//			System.out.println(n3);
//			System.out.println(n4); 
			
			System.out.println(n5); // 같은 static 이라 가능
			System.out.println(m);
		}
		
	}// end Inner
	
	
}




public class TestMain5_StaticInnerClass {

	public static void main(String[] args) {
		
		// static 이기 떄문에 Outer3로 접근
		Outer3.Inner i = new Outer3.Inner();
		i.innerMethod();
	}

}