πŸ“š[Java] static, final

텁텁·2025λ…„ 4μ›” 15일

static

  • static ν‚€μ›Œλ“œλŠ” λͺ¨λ“  객체가 ν•¨κ»˜ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜λ‚˜ λ©”μ„œλ“œλ₯Ό λ§Œλ“€λ•Œ μ‚¬μš©λœλ‹€.
  • 객체(μΈμŠ€ν„΄μŠ€)λ₯Ό λ§Œλ“€μ§€ μ•Šμ•„λ„ 클래슀 이름을 톡해 μ‚¬μš© κ°€λŠ₯
  • λͺ¨λ“  객체가 같은 값을 κ³΅μœ ν•œλ‹€.
  • staticλ³€μˆ˜μ™€ λ©”μ„œλ“œλŠ” ν•œ 번만 μƒμ„±λ˜κ³  Method Area에 μ €μž₯λœλ‹€.

클래슀 λ³€μˆ˜μ™€ 클래슀 λ©”μ„œλ“œ

  • staticν‚€μ›Œλ“œλ₯Ό 톡해 μƒμ„±λœλ‹€.
  • μΈμŠ€ν„΄μŠ€ λ³€μˆ˜, λ©”μ„œλ“œμ™€ 달리 객체λ₯Ό μƒμ„±ν•˜μ§€ μ•Šμ•„λ„ μ‚¬μš©κ°€λŠ₯
    • 접근방법 : 클래슀λͺ….λ³€μˆ˜λͺ…, 클래슀λͺ….λ©”μ„œλ“œλͺ…()
public class Cat {
    static int cat; //클래슀 λ³€μˆ˜ (λͺ¨λ“  객체가 곡유)
    String name; //μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ (각 κ°μ²΄λ§ˆλ‹€ 닀름)
    
    public Cat(String name) {
        this.name = name;
        cat++;        //클래슀 λ³€μˆ˜λŠ” 객체 없이도 μ ‘κ·Ό κ°€λŠ₯
    }
    
    public void showCat(){ //μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ
        System.out.println("κ³ μ–‘μ΄μ˜ 이름: " + name);
    }
    
    public static void printCat() { //클래슀 λ©”μ„œλ“œ
        System.out.println("κ³ μ–‘μ΄μ˜ μˆ˜λŠ”: " + cat);
    }
}
public class Main {
    public static void main(String[] args) {
        Cat.printCat(); //클래슀 λ©”μ„œλ“œ 호좜 -> 0

        Cat cat1 = new Cat("κΈΈλƒ₯이1"); //
        Cat cat2 = new Cat("κΈΈλƒ₯이2");

        cat1.showCat(); //μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ 호좜
        cat2.showCat(); //μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ 호좜

        Cat.printCat(); //클래슀 λ©”μ„œλ“œ 호좜 -> 2
    }
}

클래슀 λ©”μ„œλ“œ 주의점

  • 클래슀 λ©”μ„œλ“œμ—μ„œλŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ‚˜ λ©”μ„œλ“œμ— μ ‘κ·Όν•  수 μ—†λ‹€.
    • 객체가 없기에 this도 μ—†λ‹€.
    • 였직 클래슀 λ³€μˆ˜λ‚˜ λ‹€λ₯Έ static λ©”μ„œλ“œλ§Œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.

static은 κΌ­ ν•„μš”ν•  λ•Œλ§Œ μ‚¬μš©ν•˜μž

  • static λ³€μˆ˜μ™€ λ©”λͺ¨λ¦¬λŠ” ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλ λ•ŒκΉŒμ§€ λ©”λͺ¨λ¦¬μ— μœ μ§€λœλ‹€.
    • static λ‚¨μš©μ€ λ©”λͺ¨λ¦¬ λ‚­λΉ„λ‘œ 이어진닀.

final

  1. λ³€μˆ˜λ₯Ό λ³€κ²½ν•  수 μ—†κ²Œ λ§Œλ“ λ‹€.
  2. ν΄λž˜μŠ€λŠ” 상속할 수 μ—†κ²Œ λ§Œλ“ λ‹€.
  3. λ©”μ„œλ“œλŠ” μˆ˜μ •ν•  수 μ—†κ²Œ λ§Œλ“ λ‹€.(μ˜€λ²„λΌμ΄λ”© λΆˆκ°€)

μƒμˆ˜(Constant)

  • λ³€ν•˜μ§€ μ•Šκ³  μΌμ •ν•œ 값을 κ°–λŠ” 수
  • Javaμ—μ„œ μƒμˆ˜λŠ” λŒ€λ¬Έμžλ‘œ ν‘œκΈ°ν•˜λŠ” 것이 관둀이닀.
  • ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 쀑 μ ˆλŒ€ λ³€κ²½λΌμ„œλŠ” μ•ˆλ˜λŠ” κ°’μœΌλ‘œ static final ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ μ„ μ–Έν•œλ‹€.
  • static 으둜 μ„ μ–Έλœ λ³€μˆ˜λŠ” ν”„λ‘œκ·Έλž¨ μ‹œμž‘μ‹œ ν•œ 번만 μ΄ˆκΈ°ν™”λ˜κ³  λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œ 같은 값을 κ³΅μœ ν•œλ‹€.
  • λŒ€ν‘œμ μΈ μƒμˆ˜μ˜ μ˜ˆμ‹œ: PI(μ›μ£Όμœ¨)
    public class Circle {
        final static double PI = 3.14159; // βœ… μƒμˆ˜ μ„ μ–Έ
    }
    System.out.println("μƒμˆ˜ν™œμš©: " + Circle.PI);

λΆˆλ³€κ°μ²΄(Immutable Object)

  • 객체의 속성 값은 ν•œ 번 μ„€μ •λ˜λ©΄ λ³€κ²½λ˜μ§€ μ•ŠλŠ”λ‹€.
  • 객체의 ν•„λ“œλ₯Ό final, private final(ꢌμž₯)둜 μ„ μ–Έν•œλ‹€.
  • μ„Έν„°(Setter) 없이 μ„€κ³„ν•œλ‹€.
public final class Person {
    private final String name;
    private final int age;

    // μƒμ„±μž
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // κ²Œν„° λ©”μ„œλ“œ
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

λΆˆλ³€κ°μ²΄μ˜ λ³€κ²½

λΆˆλ³€ κ°μ²΄λŠ” λ‚΄λΆ€ 값을 λ°”κΏ€ 수 μ—†κΈ° λ•Œλ¬Έμ—, μˆ˜μ •μ΄ ν•„μš”ν•œ 경우 μˆ˜μ •λœ 값을 κ°€μ§„ μƒˆ 객체λ₯Ό λ§Œλ“€μ–΄ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

public final class Person {
    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter
    public String getName() { return name; }
    public int getAge() { return age; }

    // 값을 λ°”κΎΈλŠ” λŒ€μ‹  μƒˆλ‘œμš΄ 객체λ₯Ό λ§Œλ“€μ–΄ 리턴
    public Person withAge(int newAge) {
        return new Person(this.name, newAge);
    }
}

profile
μ°¨κ·Όμ°¨κ·Ό

0개의 λŒ“κΈ€