๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[D+70] Nested Class

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 12. 29.

 

 

๊ตญ๋น„์ง€์› D+70

-  Nested Class -

 


 

Nested Class

ํด๋ž˜์Šค๋ฅผ ํฌ๊ฒŒ ๋‘๊ฐ€์ง€๋กœ ๋‚˜๋ˆˆ๋‹ค๋ฉด Outer Class์™€ Nested Class๊ฐ€ ์žˆ๋‹ค.

Outer Class๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ‰์†Œ์— ๋ฐฐ์›Œ์™”๋˜ ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์•Œ์•„์•ผํ• ๊ฒƒ์€ ์—†๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฒˆ์— Nested Class์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ณ  ์ดํ•ดํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๋„๋กœ ํ•˜๋‹ค.

 

• Nested Class์˜ ์‚ฌ์šฉ ๋ชฉ์ 

- ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ํด๋ž˜์Šค์˜ ์ •์˜(์กด์žฌ ์ž์ฒด)๋ฅผ ๊ฐ์ถ”๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

- ๋‚ด๋ถ€์ ์œผ๋กœ ๊ตฌ์กฐ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ์‚ฌ์šฉ์ž์—๊ฒŒ ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

- ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๊ธฐ ํŽธํ•˜๋‹ค. (๋ณด๊ธฐ ํŽธํ•˜๋‹ค๋Š” ๋ง์€ ๊ด€์‹ฌ์ด ์—†๋‹ค๋Š” ์˜๋ฏธํ•˜๊ณ ๋Š” ๋‹ค๋ฅด๋‹ค.)

 

• Nested Class

class Outer		// Outer Class
{
	class Nested	// Nested Class
    {
    	...
    }
}

Nested Class๋Š” ํด๋ž˜์Šค์•ˆ์— ์กด์žฌํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

์œ ํ˜•

- Static Nested Class
- Non-static Nested Class (Inner Class)

Nested Class๋Š” ๋จผ์ € ์ „์—ญํ™”ํ•œ ํด๋ž˜์Šค์™€ ๊ทธ๋ ‡์ง€์•Š์€ ํด๋ž˜์Šค 2๊ฐœ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

 

• Static Nested Class

class Outer
{
	private static int num = 0;
    
	static class StaticNested
    {
    	...
    }
}

public class Main
{
	public static void main(String[] args)
    {
    	// ๊ฐ์ฒด์ƒ์„ฑ
    	Outer.StaticNested ns = new Outer.StaticNested();
    }
}

Static Nested Class๋Š” ์˜๋ฏธ๊ทธ๋Œ€๋กœ ์ „์—ญํ™”ํ•œ ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ Global ์˜์—ญ์— ์ธ์Šคํ„ด์Šค๋ฅผ ํ• ๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

์ƒ์„ฑ

Outer ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ๋ฌด๊ด€ํ•˜๋ฉฐ, ๋ณ„๋„๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ™œ์šฉ

Outer ํด๋ž˜์Šค์˜ static ๋ณ€์ˆ˜๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

Outer ํด๋ž˜์Šค์˜ static ๋ณ€์ˆ˜๋ฅผ ๊ณต์œ ํ•œ๋‹ค.

โžก๏ธ static ๋ณ€์ˆ˜๋Š” ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, Outer์˜ ์ผ๋ฐ˜๋ณ€์ˆ˜๋Š” static Nested class์—์„œ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

 

• Inner Class

- Member (Inner) Class
- Local (Inner) Class
- Anonymous (Inner) Class

๋„ค์Šคํ‹ฐ๋“œ ํด๋ž˜์Šค์ค‘์—์„œ static์ด ๋ถ™์ง€ ์•Š๋Š” non-static nested class๋ฅผ ๋ถ€๋ฅด๋Š”๋ง์ด๋ฉฐ, Inner Class์—๋Š” ์„ธ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

๊ฐ ํด๋ž˜์Šค๋Š” ์ •์˜๋˜์–ด ์žˆ๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ๋ถ€๋ฅธ๋‹ค.

 

• Inner Class - Member (Inner) Class

class Outer
{
	class MemberInner	// Member Inner Class
    {
    	...
    }
}

Outer Class์˜ ํ•˜์œ„์— ์ง์ ‘์ ์œผ๋กœ ์†ํ•ด์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

• Inner Class - Local (Inner) Class

class Outer
{
	void MyMethod()
    {
    	class LocalInner	// Local Inner Class
        {
        	...
        }
    }
}

Outer Class์˜ ๋ฉ”์†Œ๋“œ์•ˆ์— ์†ํ•ด์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

• Inner Class - Anonymous Inner Class

class Outer
{
	String MyMethod()
    {
    	return new ์ธํ„ฐํŽ˜์ด์Šค()
        	{
        		...
        	};
    }
}

ํด๋ž˜์Šค๋ช…์„ ์ง์ ‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค๋กœ Local Inner Class์— ํฌํ•จ๋˜๋Š” ์˜์—ญ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

Anonymous Inner Class๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ์–ด์•ผํ•˜๋ฉฐ, ์ด๋•Œ ์ธํ„ฐํŽ˜์ด์Šค๋‚ด์— ๋ฉ”์†Œ๋“œ๊ฐ€ 1๊ฐœ๊ฐ€ ์žˆ์–ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€