Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[32] 230213 Java Ch. 6 ํด๋ž˜์Šค: 5. ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 32์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[32] 230213 Java Ch. 6 ํด๋ž˜์Šค: 5. ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 32์ผ]

yjyuwisely 2023. 2. 13. 12:48

230213 Mon 32nd class

Ch. 6 ํด๋ž˜์Šค
์ง„๋„: p. 270 ~ (๊ต์žฌ: ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”, ์ €์ž: ์‹ ์šฉ๊ถŒ)

 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ” | ์‹ ์šฉ๊ถŒ - ๊ต๋ณด๋ฌธ๊ณ 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ” | ํ˜ผ์ž ํ•ด๋„ ์ถฉ๋ถ„ํ•˜๋‹ค! 1:1 ๊ณผ์™ธํ•˜๋“ฏ ๋ฐฐ์šฐ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž์Šต์„œ (JAVA 8 &11 ์ง€์›) ์ด ์ฑ…์€ ๋…ํ•™์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ž…๋ฌธ์ž๊ฐ€ ‘๊ผญ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ’ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค. 


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

์ƒ์„ฑ์ž(constructor): new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœ๋˜๋Š” ํŠน๋ณ„ํ•œ ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก.
๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ, ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋˜์–ด ์žˆ๊ณ  ๋ฐ˜ํ™˜ํ˜•์ด ์—†๋‹ค.
๊ฐ์ฒด ์ดˆ๊ธฐํ™”๋ž€ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜, ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๋ฅผ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. 

์ฐธ๊ณ : https://www.geeksforgeeks.org/java-main-method-public-static-void-main-string-args/

1. Public 

 It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.

2. Static (์ •์ )

 It is a keyword that is when associated with a method, making it a class-related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.

3. Void 

 It is a keyword and is used to specify that a method doesn’t return anything. As the main() method doesn’t return anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence, it doesn’t make any sense to return from the main() method as JVM can’t do anything with the return value of it.

4. main 

 It is the name of the Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.



Ch. 6 ํด๋ž˜์Šค

์ฑ…์˜ ๋ชฉ์ฐจ
06-1 ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
06-2 ํ•„๋“œ
06-3 ์ƒ์„ฑ์ž
06-4 ๋ฉ”์†Œ๋“œ
06-5 ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„
06-6 ํŒจํ‚ค์ง€์™€ ์ ‘๊ทผ ์ œํ•œ์ž

6.5 ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„ p. 270

์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„
class A{
     //์ธ์Šคํ„ด์Šค ํ•„๋“œ
     String color;

     //์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
     void run(){
     }
}

์ •์ (ํด๋ž˜์Šค)๋ฉค๋ฒ„ 
class A{
     //์ •์  ํ•„๋“œ
     static String color;
     //์ •์  ๋ฉ”์„œ๋“œ
     static void run(){

     }

6.5.1 ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this p. 271

์ธ์Šคํ„ด์Šค(instance) ๋ฉค๋ฒ„: ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ž€ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•˜๋Š”๋ฐ, ์ด๋“ค์€ ๊ฐ๊ฐ ์ธ์Šคํ„ด์Šค ํ•„๋“œ, ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์ธ์Šคํ„ด์Šค ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์— ์†Œ์†๋œ ๋ฉค๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด ์—†์ด๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this 1) ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„ ์„ ์–ธ

์ธ์Šคํ„ด์Šค(instance): ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด

ex) 52์žฅ์˜ ์นด๋“œ์˜ ๋ชจ์–‘, ์ˆซ์ž๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ: ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋กœ ๋‘”๋‹ค.

์ธ์Šคํ„ด์Šค ํ•„๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด ๋ฉ”์„œ๋“œ ์—ญ์‹œ ๊ฐ์ฒด ์—†์ด๋Š” ์‹คํ–‰ํ•  ์ˆ˜ ์—†๋‹ค.


์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this 2) this p. 272

๊ฐ์ฒด ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ๋„ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด this๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ž์‹ ์„ '๋‚˜'๋ผ๊ณ  ๊ฐ€๋ฆฌํ‚ค๋“ฏ์ด ๊ฐ์ฒด๋Š” ์ž์‹ ์„ this๋ผ๊ณ  ํ•œ๋‹ค.
ex) this.model = ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” model ํ•„๋“œ

this๋Š” ์ฃผ๋กœ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ํ•„๋“œ์™€ ๋™์ผํ•œ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์ธ ํ•„๋“œ๋ฅผ ๋ช…์‹œํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

๋‹ค์Œ์€ ๋งค๊ฐœ ๋ณ€์ˆ˜ model์˜ ๊ฐ’์„ ํ•„๋“œ model์— ์ €์žฅํ•œ๋‹ค.

Car(String model){
	this.model = model;
}
void setModel(String model){
	this.model = model;
}



์˜ˆ์‹œ) ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this: Car.java

public class Car3 {
	//ํ•„๋“œ
	String model;
	int speed;
	
	//์ƒ์„ฑ์ž
	Car3(String model){
		this.model = model; 
	}
	
	//๋ฉ”์„œ๋“œ
	void setSpeed(int speed) {
		this.speed = speed;
	}
	
	void run() {
		for(int i = 10; i<=50; i+=10) {
			this.setSpeed(i);
			System.out.println(this.model +"๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:" + this.speed + "km/h)");			
		}
	}
}

์˜ˆ์‹œ) ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this: CarExample.java

public class Car3Example {
	public static void main(String[] args) {
		Car3 myCar = new Car3("ํฌ๋ฅด์‰");
		Car3 yourCar = new Car3("๋ฒค์ธ ");
		
		myCar.run();
		yourCar.run();
	}
}

๊ฒฐ๊ณผ)

ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:10km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:20km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:30km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:40km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:50km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:10km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:20km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:30km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:40km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:50km/h)

6.5.2 ์ •์  ๋ฉค๋ฒ„์™€ static

- ์–ด๋–ค ๊ฐ์ฒด์— ์†Œ์†๋˜์–ด์žˆ๋‹ค๋Š” ๋œป์€ new ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ์•ผ๋งŒ ํ•ด๋‹น ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. static ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ๊ฐ์ฒด ์ƒ์„ฑ ์—†์ด๋„ ๋ฉ”์„œ๋“œ๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฐธ๊ณ : https://jooona.tistory.com/m/164

์ •์ (static): ๊ณ ์ •๋œ์ด๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ์ •์  ๋ฉค๋ฒ„๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

์ •์  ๋ฉค๋ฒ„: ํด๋ž˜์Šค์— ๊ณ ์ •๋œ ๋ฉค๋ฒ„๋กœ์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค. 
๊ฐ๊ฐ ์ •์  ํ•„๋“œ, ์ •์  ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
์ •์ ๋ฉค๋ฒ„๋Š” ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค์— ์†Œ์†๋œ ๋ฉค๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ๋ฉค๋ฒ„๋ผ๊ณ ๋„ ํ•œ๋‹ค. 

์ •์  ๋ฉค๋ฒ„์™€ static 1) ์ •์  ๋ฉค๋ฒ„ ์„ ์–ธ

์ •์  ํ•„๋“œ์™€ ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜๋ ค๋ฉด ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ static ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ถ™์ด๋ฉด ๋œ๋‹ค.

public class ํด๋ž˜์Šค{
     //์ •์  ํ•„๋“œ
     static ํƒ€์ž… ํ•„๋“œ [= ์ดˆ๊ธฐ๊ฐ’];
     
     //์ •์  ๋ฉ”์„œ๋“œ
     static ๋ฆฌํ„ด ํƒ€์ž… ๋ฉ”์„œ๋“œ (๋งค๊ฐœ๋ณ€์ˆ˜์„ ์–ธ, ...){...}
}

๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•  ๋ฐ์ดํ„ฐ: ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋กœ ์„ ์–ธ ex) ์ƒ‰๊น”์ด ๋‹ค๋ฅด๋‹ค๋ฉด color
๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ณต์šฉ ๋ฐ์ดํ„ฐ: ์ •์  ํ•„๋“œ๋กœ ์„ ์–ธ ex) ํŒŒ์ด(π)


์ •์  ๋ฉค๋ฒ„์™€ static 2) ์ •์  ๋ฉค๋ฒ„ ์‚ฌ์šฉ

ํด๋ž˜์Šค๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋กœ๋”ฉ๋˜๋ฉด ์ •์  ๋ฉค๋ฒ„๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ๋„ํŠธ(.) ์—ฐ์‚ฐ์ž๋กœ ์ ‘๊ทผํ•œ๋‹ค. 

ํด๋ž˜์Šค.ํ•„๋“œ;
ํด๋ž˜์Šค.๋ฉ”์„œ๋“œ(๋งค๊ฐœ๊ฐ’, ...);
//์ •์  ํ•„๋“œ
double result1 = 10 * 10 * Calculator.pi;
//์ •์  ๋ฉ”์„œ๋“œ
int result2 = Calculator.plus(10, 5);
int result3 = Calculator.minus(10, 5);

์˜ˆ์‹œ) ์ •์  ๋ฉค๋ฒ„ ์‚ฌ์šฉ

public class Calculator3 {
	static double pi = 3.14159;
	
	static int plus(int x, int y) {
		return x + y;
	}
	
	static int minus(int x, int y) {
		return x - y;
	}
}

์˜ˆ์‹œ) ์ •์  ๋ฉค๋ฒ„ ์‚ฌ์šฉ

public class Calculator3Example {
	public static void main(String[] args) {
		double result1 = 10 * 10 * Calculator3.pi;
		
		int result2 = Calculator3.plus(10, 5);
		
		int result3 = Calculator3.minus(10, 5);
		
		System.out.println("result1 : " + result1);
		System.out.println("result2 : " + result2);
		System.out.println("result3 : " + result3);
	}
}

๊ฒฐ๊ณผ)

result1 : 314.159
result2 : 15
result3 : 5

์ •์  ๋ฉค๋ฒ„์™€ static 3) ์ •์  ๋ฉค๋ฒ„ ์„ ์–ธ ์‹œ ์ฃผ์˜ํ•  ์  p. 277

๊ฐ์ฒด๊ฐ€ ์—†์–ด๋„ ์‹คํ–‰๋œ๋‹ค๋Š” ํŠน์ง• ๋•Œ๋ฌธ์— ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ๋Š” ์ด๋“ค ๋‚ด๋ถ€์— ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋‚˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋˜ํ•œ ๊ฐ์ฒด ์ž์‹ ์˜ ์ฐธ์กฐ์ธ this ํ‚ค์›Œ๋“œ๋„ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. 

์ •์  ๋ฉ”์„œ๋“œ์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

static void method3{
	ClassName obj = new ClassName();
    obj.field1 = 10;
    obj.method1();
}
public class StaticMember {
	//ํ•„๋“œ
	//์ธ์Šคํ„ด์Šค ํ•„๋“œ ์„ ์–ธ
	int instanceField;
	//์ •์  ํ•„๋“œ ์„ ์–ธ
	static int staticField;
	
	//๋ฉ”์„œ๋“œ
	//์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ์„ ์–ธ
	void instanceMethod(){
		//์ธ์Šคํ„ด์Šค ํ•„๋“œ ํ˜ธ์ถœ
		this.instanceField=10;
		//์ •์  ํ•„๋“œ ํ˜ธ์ถœ
		staticField = 20;
	}
	//์ •์  ๋ฉ”์„œ๋“œ ์„ ์–ธ
	static void staticMethod(){
		//์ •์  ๋ฉ”์„œ๋“œ์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.
		StaticMember sm = new StaticMember();
	//์ธ์Šคํ„ด์Šค ํ•„๋“œ ํ˜ธ์ถœ
	sm.instanceField = 30;
	//์ •์  ํ•„๋“œ ํ˜ธ์ถœ
	staticField = 40;
	}
}

6.5.3 ์‹ฑ๊ธ€ํ†ค(private) p. 279

์ „์ฒด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ๋งŒ๋“ค๋„๋ก ๋ณด์žฅํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋œ๋‹ค๊ณ  ํ•ด์„œ ์ด ๊ฐ์ฒด๋ฅผ ์‹ฑ๊ธ€ํ†ค์ด๋ผ๊ณ  ํ•œ๋‹ค.
์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ ๋งŒํผ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์‹ฑ๊ธ€ํ†ค์„ ๋งŒ๋“ค๋ ค๋ฉด ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ new ์—ฐ์‚ฐ์ž๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋„๋ก ๋ง‰์•„์•ผ ํ•œ๋‹ค. ์ƒ์„ฑ์ž ์•ž์— private ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ๋ถ™์ด๊ณ , ํ•„๋“œ์— ์ž๊ธฐ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ static์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค. 
- ์™ธ๋ถ€์—์„œ ์ƒ์„ฑ์ž ํ˜ธ์ถœ์„ ๋ง‰๊ธฐ ์œ„ํ•ด private๋ฅผ ๋ถ™์ธ๋‹ค.
(์ž˜ ์‚ฌ์šฉ์€ ์•ˆ ํ•œ๋‹ค.)

์‹ฑ๊ธ€ํ†ค์„ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ์ด๋‹ค.

public class ํด๋ž˜์Šค{
//์ •์  ํ•„๋“œ
private static ํด๋ž˜์Šค singleton = new ํด๋ž˜์Šค();

//์ƒ์„ฑ์ž
private ํด๋ž˜์Šค(){}

//์ •์  ๋ฉ”์„œ๋“œ
static ํด๋ž˜์Šค getInstance(){
	return singleton;
	}
}

์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์–ป๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ getInstance() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜1๊ณผ ๋ณ€์ˆ˜2๋Š” ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

ํด๋ž˜์Šค ๋ณ€์ˆ˜1 = ํด๋ž˜์Šค.getInstance();
ํด๋ž˜์Šค ๋ณ€์ˆ˜2 = ํด๋ž˜์Šค.getInstance();

์˜ˆ์ œ) ์‹ฑ๊ธ€ํ†ค

public class Singleton {
	private static Singleton singleton = new Singleton();
	//ํ•„๋“œ
	//์ƒ์„ฑ์ž
	private Singleton() {}
	//๋ฉ”์„œ๋“œ
	//	๋ฆฌํ„ด ํƒ€์ž… ๋ฉ”์„œ๋“œ๋ช… ๋งค๊ฐœ ๋ณ€์ˆ˜
	//	int		add		(){}
	static Singleton getInstance() {
		return singleton;
	}
}


์˜ˆ์ œ) ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด

public class SingletonExample {
	public static void main(String[] args) {
		
		/*๊ฐ™์€ ๊ฐ์ฒด์ด๋ฏ€๋กœ ์ปดํŒŒ์ผ ์—๋Ÿฌ*/
		/*Singleton obj1 = new Singleton();
		Singleton obj2 = new Singleton();*/
		
		Singleton obj1 = Singleton.getInstance();
		Singleton obj2 = Singleton.getInstance();
		
		if(obj1 == obj2) {
			System.out.println("๊ฐ™์€ Singleton ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.");
		}else {
			System.out.println("๋‹ค๋ฅธ Singleton ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.");
		}
	}
}

๊ฒฐ๊ณผ)

๊ฐ™์€ Singleton ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

6.5.4 final ํ•„๋“œ์™€ ์ƒ์ˆ˜ p. 281

๋ณ€์ˆ˜
int a = 10;
a = 20;(o)

์ƒ์ˆ˜ //์ดˆ๊ธฐ๊ฐ’์ด ์ €์žฅ๋˜๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. 
int a = 20;
a = 30;(x)
static ๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ€์งˆ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•„๋“œ ํ˜น์€ ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋ฉ”์„œ๋“œ
the value is the same for every instance of the class.
final ํ•œ ๋ฒˆ ๊ฐ’์ด ์ •ํ•ด์ง€๊ณ  ๋‚˜๋ฉด ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋Š” ํ•„๋“œ
once the variable is assigned a value it can never be changed.
static final ๋ชจ๋“  ์˜์—ญ์—์„œ ๊ณ ์ •๋œ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์ˆ˜

์ฐธ๊ณ : https://gobae.tistory.com/3


final ํ•„๋“œ์™€ ์ƒ์ˆ˜ 1) final ํ•„๋“œ

final ํ•„๋“œ๋Š” ์ดˆ๊ธฐ๊ฐ’์ด ์ €์žฅ๋˜๋ฉด ์ด๊ฒƒ์ด ์ตœ์ข…์ ์ธ ๊ฐ’์ด ๋˜์–ด์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๋„์ค‘์— ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.

final ํƒ€์ž… ํ•„๋“œ [= ์ดˆ๊ธฐ๊ฐ’];

final ํ•„๋“œ์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•

1) ํ•„๋“œ ์„ ์–ธ ์‹œ์— ์ค€๋‹ค.
2) ์ƒ์„ฑ์ž์—์„œ ์ค€๋‹ค. 
๋ณต์žกํ•œ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์— ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์•ผํ•œ๋‹ค๋ฉด ์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค.


์˜ˆ์ œ) final ํ•„๋“œ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”

1) nation์€ ํ•ญ์ƒ ๊ณ ์ •๋œ ๊ฐ’์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ํ•„๋“œ ์„ ์–ธ ์‹œ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ "Korea"๋ฅผ ์ฃผ์—ˆ๋‹ค.
2) ssn์€ Person ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ๋ถ€์—ฌ๋˜๋ฏ€๋กœ Person ํด๋ž˜์Šค ์„ค๊ณ„ ์‹œ ์ดˆ๊ธฐ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ค„ ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ ์ƒ์„ฑ์ž ๋งค๊ฐœ๊ฐ’์œผ๋กœ ssn์„ ๋ฐ›์•„์„œ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

public class Person {
	final String nation = "Korea"; //์ƒ์ˆ˜
	final String ssn; //์ƒ์ˆ˜
	String name; //๋ณ€์ˆ˜
	//์ƒ์„ฑ์ž
	//ํด๋ž˜์Šค๋ช…(ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋ช…){}
	Person(String ssn, String name){
		this.ssn = ssn;
		this.name = name;
	}
}

์˜ˆ์ œ) final ํ•„๋“œ ํ…Œ์ŠคํŠธ

๋ณ€์ˆ˜: ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ณต๊ฐ„, ํ”„๋กœ๊ทธ๋žจ์— ์˜ํ•ด ์ˆ˜์‹œ๋กœ ๊ฐ’์ด ๋ณ€๋™๋  ์ˆ˜ ์žˆ๋‹ค.
๋ฐ˜๋ฉด ์ƒ์ˆ˜๋Š” ๊ฐ’์„ ํ•œ ๋ฒˆ ์ €์žฅํ•˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๋ณ€์ˆ˜์ด๋‹ค.

public class PersonExample {
	public static void main(String[] args) {
		Person p1 = new Person("123456-1234567", "ํ™๊ธธ๋™");
		
		System.out.print(p1.nation);
		System.out.print(p1.ssn);
		System.out.print(p1.name);
		
		//p1.nation = "usa"; ์ƒ์ˆ˜๋Š” ์ง€๊ธˆ์ฒ˜๋Ÿผ ์™ธ๋ถ€์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
		//p1.ssn = "654321-7654321"; ์ƒ์ˆ˜๋Š” ์ง€๊ธˆ์ฒ˜๋Ÿผ ์™ธ๋ถ€์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
		p1.name = "ํ™์‚ผ์›";
	}
}

๊ฒฐ๊ณผ)

Korea123456-1234567ํ™๊ธธ๋™

final ํ•„๋“œ์™€ ์ƒ์ˆ˜ 2) ์ƒ์ˆ˜ (static final) p. 282

์ƒ์ˆ˜(static final): ๋ถˆ๋ณ€์˜ ๊ฐ’ ex) ์›์ฃผ์œจ ํŒŒ์ด, ์ง€๊ตฌ์˜ ๋ฌด๊ฒŒ ๋ฐ ๋‘˜๋ ˆ 

์ƒ์ˆ˜(constant value)

์ž๋ฐ”์—์„œ ๋ถˆ๋ณ€์˜ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ํ•„๋“œ์ด๋‹ค.
๊ฐ’์„ ํ•œ ๋ฒˆ ์ €์žฅํ•˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๋ณ€์ˆ˜.
๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ด๋ฉฐ, ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๊ฐ€ ํ˜ผํ•ฉ๋œ ์ด๋ฆ„์ด๋ผ๋ฉด ์–ธ๋”๋ฐ”(_)๋กœ ๋‹จ์–ด๋“ค์„ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.
static final ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค. 

final ํ•„๋“œ๋Š” ๊ฐ์ฒด๋งˆ๋‹ค ์ €์žฅ๋˜๊ณ , ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๊ฐ’์„ ํ†ตํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค.

์ƒ์ˆ˜๋Š” static์ด๋ฉด์„œ final์ด์–ด์•ผ ํ•œ๋‹ค. static final ํ•„๋“œ๋Š” ๊ฐ์ฒด๋งˆ๋‹ค ์กด์žฌํ•˜์ง€ ์•Š๊ณ  ํด๋ž˜์Šค์—๋งŒ ์กด์žฌํ•œ๋‹ค. ํ•œ ๋ฒˆ ์ดˆ๊ธฐ ๊ฐ’์ด ์ €์žฅ๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐํ™”๋  ์ˆ˜ ์—†๋‹ค. 

static final ํƒ€์ž… ์ƒ์ˆ˜ = ์ดˆ๊ธฐ๊ฐ’;

์˜ˆ์ œ) ์ƒ์ˆ˜ ์„ ์–ธ

public class Earth {
	//์›์ฃผ์œจ์— ๋Œ€ํ•œ ๊ฐ’์€ ๋ณ€ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.(3.14159)
	//final double PI = 3.14159;
	//์ง€๊ตฌ์˜ ๋ฐ˜์ง€๋ฆ„์— ๋Œ€ํ•œ ๊ฐ’์€ ๋ณ€ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. (6400)
	static final double EARTH_RADIUS = 6400;
	//์ง€๊ตฌ์˜ ํ‘œ๋ฉด์ ์— ๋Œ€ํ•œ ๊ฐ’์€ ๋ณ€ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. (4 * ์›์ฃผ์œจ * ์ง€๊ตฌ์˜ ๋ฐ˜์ง€๋ฆ„ * ์ง€๊ตฌ์˜ ๋ฐ˜์ง€๋ฆ„)
	static final double EARTH_AREA = 4 * Math.PI * EARTH_RADIUS * EARTH_RADIUS;
	static final double EARTH_AREA2 = 4 * Math.PI * Math.pow(EARTH_RADIUS, 2);
}

์˜ˆ์ œ) ์ƒ์ˆ˜ ์‚ฌ์šฉ
Math.pow(๋ฐ‘, ์ง€์ˆ˜)๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

public class EarthExample {
	public static void main(String[] args) {
		System.out.println("์ง€๊ตฌ์˜ ๋ฐ˜์ง€๋ฆ„: "+Earth.EARTH_RADIUS+" km");
		System.out.println("์ง€๊ตฌ์˜ ํ‘œ๋ฉด์ : "+Earth.EARTH_AREA+" km^2");
		System.out.println("์ง€๊ตฌ์˜ ํ‘œ๋ฉด์ : "+Earth.EARTH_AREA2+" km^2");
	}
}

๊ฒฐ๊ณผ)

์ง€๊ตฌ์˜ ๋ฐ˜์ง€๋ฆ„: 6400.0 km
์ง€๊ตฌ์˜ ํ‘œ๋ฉด์ : 5.147185403641517E8 km^2
์ง€๊ตฌ์˜ ํ‘œ๋ฉด์ : 5.147185403641517E8 km^2

๋งˆ๋ฌด๋ฆฌ
7๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ p. 284

1) ์ธ์Šคํ„ด์Šค(instance) ๋ฉค๋ฒ„: ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ž€ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•˜๋Š”๋ฐ, ์ด๋“ค์€ ๊ฐ๊ฐ ์ธ์Šคํ„ด์Šค ํ•„๋“œ, ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

2) this: ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ๋„ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด this๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
ex) this.model์€ ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” model ํ•„๋“œ๋ผ๋Š” ๋œป์ด๋‹ค.
this๋Š” ์ฃผ๋กœ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ํ•„๋“œ์™€ ๋™์ผํ•œ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์ธ ํ•„๋“œ์ž„์„ ๋ช…์‹œํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

3) ์ •์  ๋ฉค๋ฒ„: ํด๋ž˜์Šค์— ๊ณ ์ •๋œ ๋ฉค๋ฒ„๋กœ์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค. 
๊ฐ๊ฐ ์ •์  ํ•„๋“œ, ์ •์  ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
์ •์ ๋ฉค๋ฒ„๋Š” ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค์— ์†Œ์†๋œ ๋ฉค๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ๋ฉค๋ฒ„๋ผ๊ณ ๋„ ํ•œ๋‹ค. 

4) static: ์ •์  ๋ฉค๋ฒ„๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

5) ์‹ฑ๊ธ€ํ†ค(Singleton): ์ „์ฒด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ๋งŒ๋“ค๋„๋ก ๋ณด์žฅํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋œ๋‹ค๊ณ  ํ•ด์„œ ์ด ๊ฐ์ฒด๋ฅผ ์‹ฑ๊ธ€ํ†ค์ด๋ผ๊ณ  ํ•œ๋‹ค.
์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ ๋งŒํผ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ new ์—ฐ์‚ฐ์ž๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋„๋ก ๋ง‰์•„์•ผ ํ•œ๋‹ค. ์ƒ์„ฑ์ž ์•ž์— private ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ๋ถ™์ด๊ณ , ํ•„๋“œ์— ์ž๊ธฐ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ static์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค. 

6) final ํ•„๋“œ: ์ดˆ๊ธฐ๊ฐ’์ด ์ €์žฅ๋˜๋ฉด ์ด๊ฒƒ์ด ์ตœ์ข…์ ์ธ ๊ฐ’์ด ๋˜์–ด์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๋„์ค‘์— ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ํ•„๋“œ๋ฅผ ๋งํ•œ๋‹ค.
final ํ•„๋“œ๋Š” final ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค.

7) ์ƒ์ˆ˜: ์ˆ˜ํ•™์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์›์ฃผ์œจ ํŒŒ์ด๋‚˜ ์ง€๊ตฌ์˜ ๋ฌด๊ฒŒ ๋ฐ ๋‘˜๋ ˆ ๊ฐ™์€ ๋ถˆ๋ณ€์˜ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ํ•„๋“œ๋ฅผ ์ž๋ฐ”์—์„œ๋Š” ์ƒ์ˆ˜(constance)๋ผ๊ณ  ํ•œ๋‹ค. ์ƒ์ˆ˜๋Š” static final ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค. 


Q3) p. 285
ShopService ๊ฐ์ฒด๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค. ShopServiceExample ํด๋ž˜์Šค์—์„œ ShopService์˜ getInstance() ๋ฉ”์„œ๋“œ๋กœ ์‹ฑ๊ธ€ํ†ค์„ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก ShopService ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ด๋ณธ๋‹ค.

์ฝ”๋“œ 1)

public class ShopService {
	private static ShopService singleton = new ShopService();
	//์‹ฑ๊ธ€ํ†ค์ด ๋˜๊ธฐ ์œ„ํ•œ ์ž‘์„ฑ
    //์ƒ์„ฑ์ž์˜ ์ ‘๊ทผ์ œ์–ด์ž private
	private ShopService() {}
	
	static ShopService getInstance() {
		return singleton;
	}
}


์ฝ”๋“œ 2)

public class ShopServiceExample {
	public static void main(String[] args) {
		ShopService obj1 = ShopService.getInstance();
		ShopService obj2 = ShopService.getInstance();
		
		if (obj1 == obj2) {
			System.out.println("๊ฐ™์€ ShopService ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.");
		}else {
			System.out.println("๋‹ค๋ฅธ ShopService ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.");
		}
	}
}


๊ฒฐ๊ณผ)

๊ฐ™์€ ShopService ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments