์ •์  ๋ณ€์ˆ˜๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์ •์  ๋ณ€์ˆ˜ ๋˜๋Š” ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋˜๋ฉฐ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ ์•ก์„ธ์Šคํ•˜๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Java์—์„œ ์ •์  ๋ณ€์ˆ˜ ์˜ ์‚ฌ์šฉ๊ณผ ๊ตฌํ˜„์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์‹ฌ์ธต์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค .

Java์˜ ์ •์  ๋ณ€์ˆ˜ ์˜ˆ

์ •์  ๋ณ€์ˆ˜ ์˜ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์šฉ๋„ ์ค‘ ํ•˜๋‚˜๋Š” ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ„์— ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค ์ˆ˜๋ฅผ ์ถ”์ ํ•˜๋Š” ์นด์šดํ„ฐ ๋ณ€์ˆ˜ ๋˜๋Š” ์ ˆ๋Œ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๋ณด์œ ํ•˜๋Š” CONSTANT ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์€ํ–‰ ๊ณ„์ขŒ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”. ๋‹ค์Œ ์ฝ”๋“œ ์กฐ๊ฐ์€ ์ •์  ๋ณ€์ˆ˜ nextAccountNumber๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์ƒˆ ๊ณ„์ •์— ๊ณ ์œ ํ•œ ๊ณ„์ • ๋ฒˆํ˜ธ๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์˜ˆ


// BankAccount class definition
class BankAccount {
  // Declare static variable nextAccountNumber 
  static int nextAccountNumber = 1001;

  // Declare instance variables for accountNumber and balance.
  int accountNumber;
  double balance;

  // BankAccount constructor that assigns unique account number to each instance
  BankAccount() {
    // Assign the current value of nextAccountNumber to accountNumber.
    accountNumber = nextAccountNumber;

    // Increment nextAccountNumber for the next BankAccount object.
    nextAccountNumber++;
  }

  // Method to get the account number of this BankAccount object.
  int getAccountNumber() {
    return accountNumber;
  }

  // Method to get the balance of this BankAccount object.
  double getBalance() {
    return balance;
  }

  // Method to deposit an amount into this BankAccount object.
  void deposit(double amount) {
    // Increase balance by the amount deposited.
    balance += amount;
  }

  // Method to withdraw an amount from this BankAccount object.
  void withdraw(double amount) {
    // Decrease balance by the amount withdrawn.
    balance -= amount;
  }
}

// Main method definition
class Main {
  static void main(String[] args) {
    // Create three BankAccount objects: account1, account2, and account3.
    BankAccount account1 = new BankAccount();
    BankAccount account2 = new BankAccount();
    BankAccount account3 = new BankAccount();
    
    // Display the value of the static variable nextAccountNumber after creating each BankAccount object.
    System.out.println("Value of nextAccountNumber after creating account1: " + account1.getAccountNumber());
    System.out.println("Value of nextAccountNumber after creating account2: " + account2.getAccountNumber());
    System.out.println("Value of nextAccountNumber after creating account3: " + account3.getAccountNumber());
  }
}

์‚ฐ์ถœ

account1 ์ƒ์„ฑ ํ›„ nextAccountNumber ๊ฐ’: 1001 account2 ์ƒ์„ฑ ํ›„ nextAccountNumber ๊ฐ’: 1002 account3 ์ƒ์„ฑ ํ›„ nextAccountNumber ๊ฐ’: 1003
์ด ์˜ˆ์ œ์—์„œ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋Š” BankAccount ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค 3๊ฐœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. BankAccount ์ƒ์„ฑ์ž๋Š” nextAccountNumber๋ฅผ ์ƒˆ ๊ณ„์ขŒ์— ํ• ๋‹น ํ•˜๊ณ  nextAccountNumber ๋ณ€์ˆ˜๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค . getAccountNumber ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ ๊ณ„์ขŒ์˜ ๊ณ„์ขŒ ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ ๊ณ„์ •์— ๋Œ€ํ•œ ๊ณ„์ขŒ ๋ฒˆํ˜ธ์™€ nextAccountNumber ๋ณ€์ˆ˜๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ณ„์ขŒ ๋ฒˆํ˜ธ๋Š” ๊ณ ์œ ํ•˜๋ฉฐ nextAccountNumber ๋ณ€์ˆ˜๋Š” ์ƒ์„ฑ๋œ ์ƒˆ ๊ณ„์ขŒ๋งˆ๋‹ค 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์ •์  ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์‹ฑ๊ธ€ํ†ค ๋””์ž์ธ ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค๋Š” Java ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋™์‹œ์— ์ด ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์ „์—ญ ์•ก์„ธ์Šค ์ง€์ ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค . ๋‹ค์Œ ์ฝ”๋“œ ์กฐ๊ฐ์€ ์ •์  ๋ณ€์ˆ˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์˜ˆ


public class Singleton {
    // using static variable in the singleton class
    private static Singleton instance;
    private Singleton() { }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    public void displayMessage() {
        System.out.println("This is a singleton class");
    }

    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        singleton1.displayMessage();

        Singleton singleton2 = Singleton.getInstance();
        singleton2.displayMessage();

        Singleton singleton3 = Singleton.getInstance();
        singleton3.displayMessage();
    }
}

์‚ฐ์ถœ

์ด๊ฒƒ์€ ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค ์ด๊ฒƒ์€ ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค ์ด๊ฒƒ์€ ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค
์ด ์˜ˆ์ œ์—์„œ ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ๋Š” getInstance() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Singleton ํด๋ž˜์Šค ์˜ ์ธ์Šคํ„ด์Šค 3๊ฐœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค . getInstance () ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ธ์Šคํ„ด์Šค๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. displayMessage ๋ฉ”์†Œ๋“œ ๋Š” ๋‹จ์ง€ ๋ฉ”์‹œ์ง€๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋Š” ๊ฐ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด displayMessage ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค . ๋ณด์‹œ๋‹ค์‹œํ”ผ, ์„ธ ๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋”๋ผ๋„ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ์ƒ์„ฑ๋˜์–ด ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋˜์–ด ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ„์— ๊ณต์œ ๋˜๋ฉฐ ์ •์  ๋ณ€์ˆ˜ 'instance'๊ฐ€ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Œ์„ ์ฆ๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ •์  ๋ณ€์ˆ˜ ์˜ ๊ฐ’์€ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋˜๋ฏ€๋กœ ํ•œ ์ธ์Šคํ„ด์Šค์—์„œ ๋ณ€๊ฒฝํ•œ ๋ณ€์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ๋„ ํ‘œ์‹œ๋œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ์—์„œ ์ž ์žฌ์ ์ธ ์ถฉ๋Œ๊ณผ ๋ฒ„๊ทธ๋ฅผ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด ์ •์  ๋ณ€์ˆ˜๋ฅผ ์‹ ์ค‘ํ•˜๊ฒŒ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณ„์ • ์ž”์•ก๊ณผ ๊ฐ™์ด ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์— ํŠน์ •ํ•œ ๊ฐ’์„ ๋ณด์œ ํ•˜๊ธฐ ์œ„ํ•ด ์ •์  ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐฐ์šด ๋‚ด์šฉ์„ ๊ฐ•ํ™”ํ•˜๋ ค๋ฉด Java ๊ณผ์ •์˜ ๋น„๋””์˜ค ๊ฐ•์˜๋ฅผ ์‹œ์ฒญํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

์ด๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Java์˜ ์ •์  ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์นด์šดํ„ฐ, ์ƒ์ˆ˜ ๋“ฑ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๊ฐ„์— ๊ณต์œ ๋˜๋Š” ๋ณ€์ˆ˜์— ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์‹ฑ๊ธ€ํ†ค ๋””์ž์ธ ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ •์  ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋˜๋ฏ€๋กœ ํ•œ ์ธ์Šคํ„ด์Šค์—์„œ ๋ณ€๊ฒฝํ•œ ๋ณ€์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ํ‘œ์‹œ๋œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.