6. ํด๋์ค์ ๊ฐ์ฒด2
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
}
}

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
}
}