[ ๐Ÿคž ์ค‘์ฒฉ ํด๋ž˜์Šค(inner), ์ค‘์ฒฉ ์ธํ„ฐํŽ˜์ด์Šค ]

yongcraneยท2025๋…„ 4์›” 6์ผ

์ค‘์ฒฉ ํด๋ž˜์Šค(inner), ์ค‘์ฒฉ ์ธํ„ฐํŽ˜์ด์Šค, ์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค, ๋žŒ๋‹ค

=> ๊ธฐ๋ณธ

=> ์ปดํฌ์ง€์…˜ <===> ์ค‘์ฒฉ ํด๋ž˜์Šค

๐Ÿ˜ ์ปดํฌ์ง€์…˜์ด๋ž€?

class A{

 static class C { }

 static void method() {

  C c = new C();

 }

}

์ƒ์†์€ ํ™•์žฅ ๊ฐœ๋…์ด์ง€๋งŒ ์•ˆ์ •์„ฑ์ด ๋–จ์–ด์ง„๋‹ค. ์ปดํฌ์ง€์…˜ (๋‚ด๋ถ€ํด๋ž˜์Šค) ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์–ด๋–ค ํŠน์ •ํ•œ ํด๋ž˜์Šค์™€ ํŠน์ •ํ•œ ๊ด€๊ณ„๋ฅผ ๋งบ๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

ํด๋ž˜์Šค๋“ค ๊ฐ„์— ๊ด€๊ณ„๋ฅผ ๋งบ์„ ๊ฒฝ์šฐ์—๋Š” ์š”์ฆ˜์—๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ค‘์ฒฉ ํด๋ž˜์Šค -> ๋กœ์ปฌ ํด๋ž˜์Šค(์ง€์—ญํด๋ž˜์Šค), ๋ฉค๋ฒ„ ํด๋ž˜์Šค
๋ฉค๋ฒ„ = ํ•„๋“œ + ๋ฉ”์„œ๋“œ

์ค‘์ฒฉ ํด๋ž˜์Šค => ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์„ ์–ธํ•œ ํด๋ž˜์Šค๋ฅผ ์ด์•ผ๊ธฐํ•œ๋‹ค.

=> ๋‘ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋“ค์„ ์„œ๋กœ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ์™ธ๋ถ€์—์„œ ๋ถˆํ•„์š”ํ•œ ๊ด€๊ณ„ ํด๋ž˜์Šค๋ฅผ ๊ฐ์ถค์œผ๋กœ์จ ์ฝ”๋“œ์˜ ๋ณต์žก์„ฑ์„ ์ค„์ธ๋‹ค.

// ์ค‘์ฒฉ ํด๋ž˜์Šค
class ClassName{
	class ClassName1{
    
    }

}

// ์ค‘์ฒฉ ์ธํ„ฐํŽ˜์ด์Šค
class ClassName{
	interface ClassName1{
    
    }
}

ํŒŒ์ด์ฌ => ๋žŒ๋‹ค

์ ‘๊ทผ ์ œ์–ด ์ง€์‹œ์ž ์ •๋ฆฌ


// ๋™์ผ ํŒจํ‚ค์ง€๋ผ๋Š” ๊ฐ€์ •ํ•˜์—

public class C1{
	public int x;
    protected int y;
    int z;
    private int u;
    
    protected void m() {  }
}

public class C2{
	C1 o = new C1();
    can access o.x;
    can access o.y;
    can access o.z;
    cannot access o.u;
    
    can access o.m();
}

public class C3 extends C1{
    can access x;
    can access y;
    can access z;
    cannot access u;
    
    can invoke m();
}

// ๋‹ค๋ฅธ ํŒจํ‚ค์ง€
public class C4{
	C1 o = new C1();
    can access o.x;
    cannot access o.y;
    cannot access o.z;
    cannot access o.u;
    
    cannot access o.m();
}

public class C5 extends C1{
    can access x;
    can access y;
    cannot access z;
    cannot access u;
    
    can invoke m();
}
// invoke ๋Š” ํ˜ธ์ถœํ•˜๋‹ค๋ผ๋Š” ๋œป์ด๋‹ค.

์ค‘์ฒฉ ํด๋ž˜์Šค(์„ ์–ธ ์œ„์น˜์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜๋œ๋‹ค.)

๋ฉค๋ฒ„ ํด๋ž˜์Šค

์ธ์Šคํ„ด์Šค ๋งด๋ฒ„ ํด๋ž˜์Šค

class A {
	class B {  }
}

A๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ๋งŒ Bํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค

class A {
	static class B {  }
}

A ํด๋ž˜์Šค๋กœ ๋ฐ”๋กœ ์ ‘๊ทผ ํ•  ์ˆ˜ B ํด๋ž˜์Šค

๋กœ์ปฌ ํด๋ž˜์Šค

// ์ค‘์ฒฉ ํด๋ž˜์Šค
class A {
	void method() {
    	class B{  }
    }
}

method๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Bํด๋ž˜์Šค
์ค‘์ฒฉ ํด๋ž˜์Šค๋ฅผ ์ปดํŒŒ์ผ ํ•˜๋ฉด : ๋ฉค๋ฒ„ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ์—๋Š” A $ B.class ($ ํ‘œ์‹œ๋Š” ์ค‘์ฒฉ ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธ)
: ๋กœ์ปฌ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ์—๋Š” A $1 B.class

ClassName cs;
cs = new ClassName();
๋ณ€์ˆ˜ == ์ธ์Šคํ„ด์Šค

์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„ ํด๋ž˜์Šค => static ํ‚ค์›Œ๋“œ ์—†์ด ์ค‘์ฒฉ ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ์ด์•ผ๊ธฐํ•œ๋‹ค.
=> ์ธ์Šคํ„ด์Šค ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋งŒ ์„ ์–ธ ๊ฐ€๋Šฅํ•˜๊ณ  ์ •์  ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.

class A {
	class B {
    B() { }
    int field;
    static int field2;
    void method() { }
    static void method2() {  }
    }
}

A ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ Bํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋จผ์ € Aํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  B๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
A a = new A();
A.B b = a.new B();
b.field = 5;
b.method();

// ์ด๊ฑธ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค!!!
class A {
	class B {  }
  void method() { //A class์˜ ๋ฉ”์„œ๋“œ
   B b = new B();
   b.field = 5;
   b.method();
  }
}

์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค (static ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋œ ํด๋ž˜์Šค)

class A {
	static class C {
    	C();
        int field;
        static int field2;
        void method();
        static void method2() {  }
      }
}

Aํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค C์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋ ค๊ณ  ํ•˜๋ฉด
(Aํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๊ณ , ๋ฐ”๋กœ C ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค.)

A.C c = new A.C();
c.field = 5; // ์ธ์Šคํ„ด์Šค ํ•„๋“œ
c.method(); // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
c.field2 = 5; // ์ •์  ํ•„๋“œ ์‚ฌ์šฉ
c.method2(); // ์ •์  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
A.C.field2 = 5;
A.C.method2();

๋กœ์ปฌ ํด๋ž˜์Šค

๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ๋กœ์ปฌ ํด๋ž˜์Šค๋ผ๊ณ  ํ•œ๋‹ค.
์ ‘๊ทผ ์ œ์–ด ์ง€์‹œ์ž ๋ฐ static์„ ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
(๋กœ์ปฌ ํด๋ž˜์Šค๋Š” ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ์ ‘๊ทผ์„ ์ œํ•œํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค)

void method(){
	class D {
    D();
    int field;
    static int field1; // ์‹œ์šฉ ๋ชปํ•จ
    void method() {  }
    static void method1() {   } // ์‹œ์šฉ ๋ชปํ•จ
    }
    
    D d = new D();
    d.field = 5;
    d.method();
}

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ์Šค๋ ˆ๋“œ ๊ฐ์ฒด ๋งŒ๋“ค๋–„ ๋กœ์ปฌ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

class A {
	A() {
	System.out.println("A ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ");
	
	class B{
	B(){
	System.out.println("B ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ");
	}
	int field1;
	void method1() {    }
	}

	static class C {
	C() {
	System.out.println("C ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ");
	}
	int field1;
	static int field2;
	void method1()  {   }
	static void method2()  {   }
	}

	void method() {
	class D {
		D() {
			System.out.println("D ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ");
		}
		int field1;
		void method1() {  }
	}
		D d = new D();
		d.field1 = 5;
		d.method1();
	}
}
	
	public class Main {
		public static void main(String[] args) {
		A a = new A();
		A.B = a.new B();
		b.field1 = 5;
		b.method1();

		A.C c = new A.C();
		c.field1 = 5;
		c.method1();
		A.C.field1 = 5;			
		A.C.method1();
		a.method();
	}
}
}



// errorCode๊ฐ€ ์—†๋‹ค
public class A {
	B field1 = new B();
	C field2 = new C();

 void method() {
	B var1 = new B();
	C var2 = new C();
}

 static B field3 = new B();
 static C field4 = new C();

static void method() {
	B var1 = new B();
	C var2 = new C();
}

	class B {    }
	static class C {    }
}

// errorCode๊ฐ€ ์žˆ๋‹ค
public class A {
	int field1;
	void method1() {  }

	static int field2;
	static void method2() {  }
       
     class B {
	void method() {
	field1 = 10;
	method1();

	field2 = 10;
	method2();
	}
      }
      static class C {
		void method() {
	field2 = 10;
	method2();
		}
	}
}	

โญโญโญโญโญ


// => ๋‘๊ฐœ์˜ ์ฝ”๋“œ ๊ฒฐ๊ณผ๋Š” Test Code์ด๋‹ค
pubic class Parcel2 {
	class Contents {
	private int i = 10;
	public int value() {
		return i;
		}
	}
	class Destination {
		private String label;
		Destination(String where To) [
		label = where To;
		}	
		String readLabel(){
		return label;	
		}
	}
       public Destion to(String s) {
    	return new Destination(s);
    }
    public Contents contents() {
    	return new Contents();
    }
	public void ship(String dest){
		Contents c = new Contents();
        Destination ds = to(dext);
		Destination d = new Destination(dest);
		System.out.println(d.readLabel());
	}
	public static void main(String[] args) {
		Parcle2 P = new PArcel2();
		p.ship("Test Code");
        Parcel2 q = new Parcel2();
        Parcel2.Contents c = q.contents();
        Parcel2.Destination d = q.to("Testing Code");
        
	}
}

์™ธ๊ณฝ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ์—์„œ ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ”๋กœ ์ฐธ์กฐ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€? ํ˜น์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š”์ง€์š”??

โœ… ๊ฒฐ๋ก  ์š”์•ฝ
์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค(Non-static inner class):
์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ์—์„œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ฐธ์กฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค(Static nested class):
์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์—์„œ ์ •์ ์ด๋ฏ€๋กœ ๋ฐ”๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

profile
์งง๊ณ  ๊ฐ•๋ ฅํ•˜๊ฒŒ!

0๊ฐœ์˜ ๋Œ“๊ธ€