ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/์ž๋ฐ”

6. ํด๋ž˜์Šค์™€ ๊ฐ์ฒด2

alswlfl 2026. 6. 12. 23:10

this ์˜ˆ์•ฝ์–ด

1. ์ž์‹ ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” this

ํด๋ž˜์Šค ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” this: ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์˜ˆ์•ฝ์–ด

package thisex;

class BirthDay {
	int day;
	int month;
	int year;
	
	public void setYear(int year) {
		this.year = year;
	}
	
	public void printThis() {
		System.out.println(this);
	}
}

public class ThisExample {
	public static void main(String[] args) {
		BirthDay bDay = new BirthDay();
		bDay.setYear(2000);
		System.out.println(bDay); // thisex.BirthDay@50040f0c
		bDay.printThis(); // thisex.BirthDay@50040f0c
	}
}

ํด๋ž˜์Šค ๋‚ด์˜ this

2. ์ƒ์„ฑ์ž์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” this

ํด๋ž˜์Šค์— ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๋•Œ ์ƒ์„ฑ์ž์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ this๋ฅผ ์‚ฌ์šฉํ•จ

package thisex;

class Person {
	String name;
	int age;
	
	Person() {
		this("์ด๋ฆ„ ์—†์Œ",1); // this๋ฅผ ์‚ฌ์šฉํ•ด Person(String, int) ์ƒ์„ฑ์ž ํ˜ธ
	}
	
	Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
}

public class CallAnotherConst {
	public static void main(String[] args) {
		Person noName = new Person();
		System.out.println(noName.name); // ์ด๋ฆ„ ์—†์Œ
		System.out.println(noName.age); // 1
	}
}
  • this๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ ์ด์ „์— ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์—†์Œ! ์ฆ‰, this๋ฅผ ํ™œ์šฉํ•œ ๋ฌธ์žฅ์ด ๊ฐ€์žฅ ๋จผ์ € ์™€์•ผ ํ•จ

3. ์ž์‹ ์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” this

์ƒ์„ฑ๋œ ํด๋ž˜์Šค ์ž์‹ ์˜ ์ฃผ์†Œ ๊ฐ’์„ this๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ

package thisex;

class Person {
	String name;
	int age;
	
	Person() {
		this("์ด๋ฆ„ ์—†์Œ",1); // this๋ฅผ ์‚ฌ์šฉํ•ด Person(String, int) ์ƒ์„ฑ์ž ํ˜ธ
	}
	
	Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	// ๋ฐ˜ํ™˜ํ˜•์€ ํด๋ž˜์Šคํ˜•
	Person returnItSelf() {
		return this; // this ๋ฐ˜
	}
}

public class CallAnotherConst {
	public static void main(String[] args) {
		Person noName = new Person();
		System.out.println(noName.name); // ์ด๋ฆ„ ์—†์Œ
		System.out.println(noName.age); // 1
		
		Person p = noName.returnItSelf(); // this ๊ฐ’์„ ํด๋ž˜์Šค ๋ณ€์ˆ˜์— ๋Œ€์ž…
		System.out.println(p); // noName.returnItSelf()์˜ ๋ฐ˜ํ™˜ ๊ฐ’ ์ถœ๋ ฅ, thisex.Person@f6f4d33
		System.out.println(noName); // ์ฐธ์กฐ ๋ณ€์ˆ˜ ์ถœ๋ ฅ, thisex.Person@f6f4d33
	}
}

๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ

ex) ํ•™์ƒ, ๋ฒ„์Šค, ์ง€ํ•˜์ฒ  ์„ธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ํ•™์ƒ์ด ๋ฒ„์Šค๋‚˜ ์ง€ํ•˜์ฒ ์„ ํƒ€๊ณ  ํ•™๊ต์— ๊ฐ€๋Š” ๊ฒƒ ๋งŒ๋“ค๊ธฐ

package cooperation;

// ํ•™์ƒ ํด๋ž˜์Šค ๊ตฌํ˜„ 
public class Student {
	public String studentName; // ํ•™์ƒ ์ด๋ฆ„ 
	public int grade; // ํ•™๋…„ 
	public int money; // ํ•™์ƒ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ˆ 
	
	public Student(String studentName, int money) {
		this.studentName = studentName;
		this.money = money;
	}
	
	// ํ•™์ƒ์ด ๋ฒ„์Šค๋ฅผ ํƒ€๋ฉด 1,000์›์„ ์ง€๋ถˆํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ 
	public void takeBus(Bus bus) {
		bus.take(1000);
		this.money -= 1000;
	}
	
	// ํ•™์ƒ์ด ์ง€ํ•˜์ฒ ์„ ํƒ€๋ฉด 1,500์›์„ ์ง€๋ถˆํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ
	public void takeSubway(Subway subway) {
		subway.take(1500);
		this.money -= 1500;
	}
	
	public void showInfo() {
		System.out.println(studentName + "๋‹˜์˜ ๋‚จ์€ ๋ˆ์€" + money + "์ž…๋‹ˆ๋‹ค.");
	}
}
package cooperation;

// ๋ฒ„์Šค ํด๋ž˜์Šค ๊ตฌํ˜„
public class Bus {
	int busNumber; // ๋ฒ„์Šค ๋ฒˆํ˜ธ
	int passengerCount; // ์Šน๊ฐ ์ˆ˜
	int money; // ๋ฒ„์Šค ์ˆ˜์ž…
	
	public Bus(int busNumber) {
		this.busNumber = busNumber;
	}
	
	public void take(int money) {
		this.money += money; // ๋ฒ„์Šค ์ˆ˜์ž… ์ฆ๊ฐ€
		passengerCount++; // ์Šน๊ฐ ์ˆ˜ ์ฆ๊ฐ€
	}
	
	public void showInfo() {
		System.out.println("๋ฒ„์Šค " + busNumber + "๋ฒˆ์˜ ์Šน๊ฐ์€ " + passengerCount + "๋ช…์ด๊ณ , ์ˆ˜์ž…์€ " + money + "์ž…๋‹ˆ๋‹ค.");
	}
}
package cooperation;

// ์ง€ํ•˜์ฒ  ํด๋ž˜์Šค ๊ตฌํ˜„
public class Subway {
	String lineNumber; // ์ง€ํ•˜์ฒ  ๋…ธ์„ 
	int passengerCount; // ์Šน๊ฐ ์ˆ˜
	int money; // ์ˆ˜์ž…์•ก
	
	public Subway(String lineNumber) {
		this.lineNumber = lineNumber;
	}
	
	public void take(int money) {
		this.money += money; // ์ˆ˜์ž… ์ฆ๊ฐ€
		passengerCount++; // ์Šน๊ฐ ์ˆ˜ ์ฆ๊ฐ€
	}
	
	public void showInfo() {
		System.out.println(lineNumber + "์˜ ์Šน๊ฐ์€ " + passengerCount + "๋ช…์ด๊ณ , ์ˆ˜์ž…์€ " + money + "์ž…๋‹ˆ๋‹ค.");
	}
}
package cooperation;

public class TakeTrans {

	public static void main(String[] args) {
		Student studentJames = new Student("James", 5000);
		Student studentTomas = new Student("Tomas", 10000);
		
		Bus bus100 = new Bus(100);
		studentJames.takeBus(bus100); // james๊ฐ€ 100๋ฒˆ ๋ฒ„์Šค๋ฅผ ํƒ
		studentJames.showInfo(); // James๋‹˜์˜ ๋‚จ์€ ๋ˆ์€4000์ž…๋‹ˆ๋‹ค.
		bus100.showInfo(); // ๋ฒ„์Šค 100๋ฒˆ์˜ ์Šน๊ฐ์€ 1๋ช…์ด๊ณ , ์ˆ˜์ž…์€ 1000์ž…๋‹ˆ๋‹ค.
		
		Subway subwayGreen = new Subway("2ํ˜ธ์„ ");
		studentTomas.takeSubway(subwayGreen); // tomas๊ฐ€ 2ํ˜ธ์„ ์„ ํƒ
		studentTomas.showInfo(); // Tomas๋‹˜์˜ ๋‚จ์€ ๋ˆ์€8500์ž…๋‹ˆ๋‹ค.
		subwayGreen.showInfo(); // 2ํ˜ธ์„ ์˜ ์Šน๊ฐ์€ 1๋ช…์ด๊ณ , ์ˆ˜์ž…์€ 1500์ž…๋‹ˆ๋‹ค.
	}

}

static ๋ณ€์ˆ˜

static ๋ณ€์ˆ˜๋Š” ์ •์  ๋ณ€์ˆ˜์ž„

static ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜์ด๋ฆ„;
  • static ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์„ ์–ธํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹˜
  • static ๋ณ€์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ”์„ ๋•Œ ๋”ฑ ํ•œ ๋ฒˆ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ํ• ๋‹น๋˜๊ณ , ๊ทธ ๊ฐ’์€ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณต์œ ํ•จ
  • static์œผ๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€์—†์ด ๋จผ์ € ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ๊ฐ’์„ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณต์œ ํ•˜๊ฒŒ ๋˜์–ด static ๋ณ€์ˆ˜๋ฅผ 'ํด๋ž˜์Šค ๋ณ€์ˆ˜'๋ผ๊ณ ๋„ ํ•จ
package staticex;

public class Student {
	public static int serialNum = 1000; // static ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์ƒ๊ด€์—†์ด ๋จผ์ € ์ƒ์„ฑ๋จ
	public int studentID;
	public String studentName;
	public int grade;
	public String address;
	
	public String getStudentName() {
		return studentName;
	}
	
	public void setStudentName(String name) {
		studentName = name;
	}
}
package staticex;

public class StudentTest1 {

	public static void main(String[] args) {
		Student studentLee = new Student();
		studentLee.setStudentName("์ด์ง€์›");
		System.out.println(studentLee.serialNum); // serialNum ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐ๊ฐ’ ์ถœ๋ ฅ, 1000
		studentLee.serialNum++; // static ๋ณ€์ˆ˜ ๊ฐ’ ์ฆ๊ฐ€
		
		Student studentKim = new Student();
		studentKim.setStudentName("๊น€์ฒ ์ˆ˜");
		System.out.println(studentKim.serialNum); // 1001
		System.out.println(studentLee.serialNum); // 1001
	}
}

 

์œ„ ์ฝ”๋“œ์—์„œ static์œผ๋กœ ์„ ์–ธํ•œ serialNum ๋ณ€์ˆ˜๋Š” ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‘ ๊ฐœ์˜ ์ฐธ์กฐ ๋ณ€์ˆ˜๊ฐ€ ๋™์ผํ•œ ๋ณ€์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ด

ํด๋ž˜์Šค ๋ณ€์ˆ˜

static ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ๋ณ„๊ฐœ์ด๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค๋ณด๋‹ค ๋จผ์ € ์ƒ์„ฑ๋จ. ๋”ฐ๋ผ์„œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ๋„ ์ฐธ์กฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

package staticex;

public class Student {
	public static int serialNum = 1000; // static ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์ƒ๊ด€์—†์ด ๋จผ์ € ์ƒ์„ฑ๋จ
	public int studentID;
	public String studentName;
	public int grade;
	public String address;
	
	public Student() {
		serialNum++; // ํ•™์ƒ์ด ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€
		studentID = serialNum; // ์ฆ๊ฐ€๋œ ๊ฐ’์„ ํ•™๋ฒˆ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ๋ถ€์—ฌ
	}
	
	public String getStudentName() {
		return studentName;
	}
	
	public void setStudentName(String name) {
		studentName = name;
	}
}
package staticex;

public class StudentTest1 {

	public static void main(String[] args) {
		Student studentLee = new Student();
		studentLee.setStudentName("์ด์ง€์›");
		System.out.println(Student.serialNum); // 1001
		
		Student studentKim = new Student();
		studentKim.setStudentName("๊น€์ฒ ์ˆ˜");
		System.out.println(Student.serialNum); // 1002
	}
}

ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ

static ๋ณ€์ˆ˜๋ฅผ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ static ๋ฉ”์„œ๋“œ, ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•จ

package staticex;

public class Student {
	private static int serialNum = 1000; // private ๋ณ€์ˆ˜๋กœ ๋ณ€๊ฒฝ
	public int studentID;
	public String studentName;
	public int grade;
	public String address;
	
	public Student() {
		serialNum++; // ํ•™์ƒ์ด ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€
		studentID = serialNum; // ์ฆ๊ฐ€๋œ ๊ฐ’์„ ํ•™๋ฒˆ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ๋ถ€์—ฌ
	}
	
	public String getStudentName() {
		return studentName;
	}
	
	public void setStudentName(String name) {
		studentName = name;
	}
	
	public static int getSerialNum() {
		int i = 10;
		return serialNum;
	}
	
	public static void setSerialNum(int serialNum) {
		Student.serialNum = serialNum;
	}
}
package staticex;

public class StudentTest1 {

	public static void main(String[] args) {
		Student studentLee = new Student();
		studentLee.setStudentName("์ด์ง€์›");
		System.out.println(Student.getSerialNum()); // 1001
		System.out.println(studentLee.studentName + " ํ•™๋ฒˆ:" + studentLee.studentID);
		
		Student studentKim = new Student();
		studentKim.setStudentName("๊น€์ฒ ์ˆ˜");
		System.out.println(Student.getSerialNum()); // 1002
		System.out.println(studentKim.studentName + " ํ•™๋ฒˆ:" + studentKim.studentID);
	}
}

 

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ static ๋ณ€์ˆ˜๋ฅผ private์œผ๋กœ ์„ ์–ธํ•ด ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์ฐธ์กฐ

ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜

ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ X

  • ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์— ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” ๊ทธ ์ง€์—ญ์—์„œ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์„œ ์ง€์—ญ๋ณ€์ˆ˜๋ผ๊ณ  ํ•จ
  • ์ง€์—ญ๋ณ€์ˆ˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋˜์–ด ๋ฉ”์„œ๋“œ๊ฐ€ ๋๋‚˜๋ฉด ์‚ฌ๋ผ์ง€๋Š” ๋ณ€์ˆ˜์ž„
  • ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋งŒ๋“ค์–ด์ง€๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, static ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ
package staticex;

public class Student {
	private static int serialNum = 1000; // private ๋ณ€์ˆ˜๋กœ ๋ณ€๊ฒฝ
	public int studentID;
	public String studentName;
	public int grade;
	public String address;
	
	public Student() {
		serialNum++; // ํ•™์ƒ์ด ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€
		studentID = serialNum; // ์ฆ๊ฐ€๋œ ๊ฐ’์„ ํ•™๋ฒˆ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ๋ถ€์—ฌ
	}
	
	public String getStudentName() {
		return studentName;
	}
	
	public void setStudentName(String name) {
		studentName = name;
	}
	
	public static int getSerialNum() {
		int i = 10;
		studentName = "์ด์ง€์›"; // Cannot make a static reference to the non-static field studentName
		return serialNum;
	}
	
	public static void setSerialNum(int serialNum) {
		Student.serialNum = serialNum;
	}
}

 

ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ์™€ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

package staticex;

public class StudentTest1 {

	public static void main(String[] args) {
		System.out.println(Student.getSerialNum()); // 1000
	}
}

๋ณ€์ˆ˜ ์œ ํšจ ๋ฒ”์œ„

1. ์ง€์—ญ ๋ณ€์ˆ˜์˜ ์œ ํšจ ๋ฒ”์œ„

ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ๋ฐ–์—์„œ๋Š” ์‚ฌ์šฉ X

  • ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์— ์„ ์–ธํ•œ ์ง€์—ญ ๋ณ€์ˆ˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ X
  • ์ง€์—ญ ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ = ์Šคํƒ(Stack)
  • ์Šคํƒ์— ์ƒ์„ฑ๋˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ƒ์„ฑ๋˜์—ˆ๋‹ค๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋˜๋ฉด ํ• ๋‹น๋˜์—ˆ๋˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ํ•ด์ œ๋˜๋ฉด์„œ ํ•จ๊ป˜ ์—†์–ด์ง

2. ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์˜ ์œ ํšจ ๋ฒ”์œ„

๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ผ๊ณ ๋„ ํ•จ. ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํž™(heap) ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋˜๋Š” ๋ณ€์ˆ˜

  • ํด๋ž˜์Šค์˜ ์–ด๋А ๋ฉ”์„œ๋“œ์—์„œ๋‚˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ํž™์— ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ์˜ํ•ด ์ˆ˜๊ฑฐ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ง
  • ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ

3. static ๋ณ€์ˆ˜์˜ ์œ ํšจ ๋ฒ”์œ„

์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์— ํ”„๋กœ๊ทธ๋žจ์ด ์ƒ์ฃผํ•จ. ์ด๋•Œ ํ”„๋กœ๊ทธ๋žจ ์˜์—ญ ์ค‘์— ๋ฐ์ดํ„ฐ ์˜์—ญ์ด ์กด์žฌํ•จ. ์ด ์˜์—ญ์— ์ƒ์ˆ˜, ๋ฌธ์ž์—ด, static ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋จ

  • ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฌธ์žฅ ์ฆ‰ new๊ฐ€ ๋˜์–ด์•ผ ์ƒ์„ฑ๋˜์ง€๋งŒ, static ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€ ์—†์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ์˜์—ญ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋จ
  • ์ƒ์„ฑ๋œ static ๋ณ€์ˆ˜๋Š” private์ด ์•„๋‹ˆ๋ผ๋ฉด ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ๋„ ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์ด ๋๋‚œ ๋’ค ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋‚ด๋ ค๊ฐ€๋ฉด static ๋ณ€์ˆ˜๋„ ์†Œ๋ฉธ๋จ
  • ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘ํ•  ๋•Œ๋ถ€ํ„ฐ ๋๋‚  ๋•Œ๊นŒ์ง€ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผํ•˜๋ฏ€๋กœ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ํฐ ๋ณ€์ˆ˜๋ฅผ static์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š์Œ
๋ณ€์ˆ˜ ์œ ํ˜• ์„ ์–ธ ์œ„์น˜ ์‚ฌ์šฉ ๋ฒ”์œ„ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ
์ง€์—ญ ๋ณ€์ˆ˜(๋กœ์ปฌ ๋ณ€์ˆ˜) ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์„ ์–ธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ์Šคํƒ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ƒ์„ฑ๋˜๊ณ  ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋ฉด ์†Œ๋ฉธํ•จ
๋ฉค๋ฒ„ ๋ณ€์ˆ˜(์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜) ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๊ณ  private์ด ์•„๋‹ˆ๋ฉด ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํž™ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํž™์— ์ƒ์„ฑ๋˜๊ณ , ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ˆ˜๊ฑฐํ•  ๋•Œ ์†Œ๋ฉธ๋จ
static ๋ณ€์ˆ˜(ํด๋ž˜์Šค ๋ณ€์ˆ˜) static ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์„ ์–ธ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๊ณ  private์ด ์•„๋‹ˆ๋ฉด ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๋ฐ์ดํ„ฐ ์˜์—ญ ํ”„๋กœ๊ทธ๋žญ๋ฏธ ์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ ์ƒ์ˆ˜์™€ ํ•จ๊ป˜ ๋ฐ์ดํ„ฐ ์˜์—ญ์— ์ƒ์„ฑ๋˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ๋๋‚˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•  ๋•Œ ์†Œ๋ฉธ๋จ

 


static ์‘์šฉ - ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(singleton pattern): ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด

 

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ํšŒ์‚ฌ ํด๋ž˜์Šค ๊ตฌํ˜„

ex) ์–ด๋–ค ํšŒ์‚ฌ์˜ ์ง์›๋“ค์„ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, ์ง์›์€ ์—ฌ๋Ÿฌ ๋ช…์ด์ง€๋งŒ ํšŒ์‚ฌ๋Š” ํ•˜๋‚˜์ž„. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ง์› ์ธ์Šคํ„ด์Šค๋Š” ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋งž์ง€๋งŒ, ํšŒ์‚ฌ ๊ฐ์ฒด๋Š” ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•ด์•ผ ํ•จ

 

๋‹จ๊ณ„ 1: ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ๋งŒ๋“ค๊ธฐ

package singleton;

public class Company {
	private Company() {}
}

 

์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ํด๋ž˜์Šค๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋””ํดํŠธ ์ƒ์„ฑ์ž ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์ง€๋งŒ, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๋””ํดํŠธ ์ƒ์„ฑ์ž๋Š” ํ•ญ์ƒ public์ž„

์ƒ์„ฑ์ž๊ฐ€ public์ด๋ฉด ์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์—์„œ๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ช…์‹œ์ ์œผ๋กœ ๋งŒ๋“ค๊ณ  ๊ทธ ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ private์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•จ

 

๋‹จ๊ณ„ 2: ํด๋ž˜์Šค ๋‚ด๋ถ€์— static์œผ๋กœ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

package singleton;

public class Company {
	private static Company instance = new Company(); // ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค
	private Company() {}
}

 

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

๋”ฐ๋ผ์„œ Company ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด, ์ด ์ธ์Šคํ„ด์Šค๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด์—์„œ ์‚ฌ์šฉํ•  ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋จ. ๋˜ํ•œ private์œผ๋กœ ์„ ์–ธํ•ด ์™ธ๋ถ€์—์„œ ์ด ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ œํ•œํ•ด์•ผ ํ•จ

 

๋‹จ๊ณ„ 3: ์™ธ๋ถ€์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” public ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ

package singleton;

public class Company {
	private static Company instance = new Company(); // ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด
	private Company() {}
	
	public static Company getInstance() {
		if(instance == null) {
			instance = new Company();
		}
		return instance; // ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค ๋ฐ˜ํ™˜
	}
}

 

private์œผ๋กœ ์„ ์–ธํ•œ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์™ธ๋ถ€์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•จ. ์ด๋ฅผ ์œ„ํ•ด public ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•จ

์ด๋•Œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ static์œผ๋กœ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•จ. getInstance() ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€์—†์ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ

 

๋‹จ๊ณ„ 4: ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ ๋งŒ๋“ค๊ธฐ

package singleton;

public class CompanyTest {

	public static void main(String[] args) {
		Company myCompany1 = Company.getInstance();
		Company myCompany2 = Company.getInstance();
		System.out.println(myCompany1 == myCompany2); // true
	}

}