Backend/JAVA

static ํ‚ค์›Œ๋“œ

dddzr 2025. 2. 26. 20:32

๐Ÿ“Œ 1. static ํ‚ค์›Œ๋“œ์˜ ์˜๋ฏธ

static์€ "์ •์ ์ธ"์ด๋ผ๋Š” ์˜๋ฏธ๋กœ,  static์„ ๋ถ™์ด๋ฉด ๊ฐ์ฒด(instance)์™€ ๊ด€๊ณ„์—†์ด ํด๋ž˜์Šค(class) ๋‹จ์œ„๋กœ ๊ด€๋ฆฌ๋œ๋‹ค๋Š” ๋œป์ด๋‹ค!

์ฆ‰, ํด๋ž˜์Šค๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋  ๋•Œ ํ•œ ๋ฒˆ๋งŒ ์ƒ์„ฑ๋˜๋ฉฐ, ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•œ๋‹ค.

 

โœ… 2. static ํ‚ค์›Œ๋“œ์˜ ์‚ฌ์šฉ

1๏ธโƒฃ static ๋ณ€์ˆ˜ (ํด๋ž˜์Šค ๋ณ€์ˆ˜)

  • ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๋ณ€์ˆ˜! (์ธ์Šคํ„ด์Šค์™€ ๊ด€๊ณ„ ์—†์Œ)
  • ํด๋ž˜์Šค๋ช….๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐Ÿ“– ์˜ˆ์ œ

class Counter {
    static int count = 0; // ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๋ณ€์ˆ˜

    Counter() {
        count++; // ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค count ์ฆ๊ฐ€
    }
}

public class Main {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();

        System.out.println(Counter.count); // ์ถœ๋ ฅ: 2
    }
}

 

โœ” count๋Š” static ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ๋ชจ๋“  Counter ๊ฐ์ฒด๊ฐ€ ๊ณต์œ 
โœ” c1, c2๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค count ๊ฐ’์ด ์ฆ๊ฐ€

โœ” Counter.count์ฒ˜๋Ÿผ ํด๋ž˜์Šค๋ช…์œผ๋กœ ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ

 

2๏ธโƒฃ static ๋ฉ”์„œ๋“œ (ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ)

  • ๊ฐ์ฒด ์—†์ด ํ˜ธ์ถœ ๊ฐ€๋Šฅ! (์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ โŒ)
  • ํด๋ž˜์Šค๋ช….๋ฉ”์„œ๋“œ๋ช…()์œผ๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜/๋ฉ”์„œ๋“œ์— ์ ‘๊ทผ ๋ถˆ๊ฐ€
  • ์ฃผ๋กœ ์œ ํ‹ธ์„ฑ ๋ฉ”์„œ๋“œ, ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉ

๐Ÿ“– ์˜ˆ์ œ

class MathUtils {
    static int square(int x) {
        return x * x;
    }
}

public class Main {
    public static void main(String[] args) {
        int result = MathUtils.square(5);
        System.out.println(result); // ์ถœ๋ ฅ: 25
    }
}

 

โŒ ์ฃผ์˜ : static ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(this.๋ณ€์ˆ˜๋ช…)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ!

โญ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ๊ฐœ๋ณ„์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ.

 

๐Ÿ“– ์˜ˆ์ œ

class Example {
    int x = 10; 
    static void printX() {
        System.out.println(x); // ์˜ค๋ฅ˜! (์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ ‘๊ทผ ๋ถˆ๊ฐ€)
    }
}

 

3๏ธโƒฃ static ๋ธ”๋ก (ํด๋ž˜์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋ก)

  • ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋จ!
  • ์ฃผ๋กœ static ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ์šฉ๋„๋กœ ์‚ฌ์šฉ
  • main() ๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋จ

๐Ÿ“– ์˜ˆ์ œ

class Config {
    static String appName;

    static { // ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ํ•œ ๋ฒˆ ์‹คํ–‰๋จ
        appName = "My App";
        System.out.println("Config Loaded!");
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Config.appName); // My App
    }
}

 

โœ” static {} ๋ธ”๋ก์€ ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ํ•œ ๋ฒˆ ์‹คํ–‰๋จ
โœ” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ •, ์ƒ์ˆ˜ ์ดˆ๊ธฐํ™” ๋“ฑ์— ์‚ฌ์šฉ

 

4๏ธโƒฃ static ๋‚ด๋ถ€ ํด๋ž˜์Šค (์ค‘์ฒฉ ํด๋ž˜์Šค)

  • ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ!
  • static ํด๋ž˜์Šค๋Š” ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๋ถˆ๊ฐ€
  • ๋ฐ”๊นฅํด๋ž˜์Šค.๋‚ด๋ถ€ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ ์ ‘๊ทผ

๐Ÿ“– ์˜ˆ์ œ

class Outer {
    static class Inner {
        void display() {
            System.out.println("Hello from static inner class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Inner obj = new Outer.Inner(); // ๊ฐ์ฒด ์ƒ์„ฑ ๊ฐ€๋Šฅ
        obj.display();
    }
}

 

๐Ÿš€ ๊ฒฐ๋ก 

 

static์€ ์ •์ (ํด๋ž˜์Šค ๋‹จ์œ„ ๊ด€๋ฆฌ)๋ผ๋Š” ๊ณตํ†ต์ ์ด ์žˆ์ง€๋งŒ, ๊ฐ๊ฐ ์ ์šฉ๋˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ„!

static ์š”์†Œ ์„ค๋ช… ํŠน์ง•
static ๋ณ€์ˆ˜ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๋ณ€์ˆ˜ ํด๋ž˜์Šค ๋กœ๋“œ ์‹œ 1๋ฒˆ๋งŒ ์ƒ์„ฑ
static ๋ฉ”์„œ๋“œ ๊ฐ์ฒด ์—†์ด ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๋ถˆ๊ฐ€
static ๋ธ”๋ก ํด๋ž˜์Šค ๋กœ๋“œ ์‹œ 1๋ฒˆ ์‹คํ–‰๋˜๋Š” ์ดˆ๊ธฐํ™” ๋ธ”๋ก main()๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰
static ๋‚ด๋ถ€ ํด๋ž˜์Šค ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ ‘๊ทผ โŒ

โœ” ๊ณต์œ ํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” static ๋ณ€์ˆ˜๋กœ!
โœ” ๊ฐ์ฒด ์—†์ด ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ๋Š” static ๋ฉ”์„œ๋“œ๋กœ!
โœ” ํด๋ž˜์Šค ๋กœ๋”ฉ ์‹œ ์ดˆ๊ธฐํ™”ํ•  ์ž‘์—…์€ static ๋ธ”๋ก์œผ๋กœ!