๐ 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 ๋ธ๋ก์ผ๋ก!
'Backend > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋ฐ์ดํฐ ํ์ Primitive Type vs Wrapped Type (0) | 2025.02.26 |
---|---|
JNDI๋? (0) | 2025.02.26 |
์๋ฐ(Spring) ๊ธฐ๋ณธ ์์ธ ์ฒ๋ฆฌ (0) | 2025.02.23 |
[Java] ๋น๋๊ธฐ ์์ ์ฒ๋ฆฌ (ExecutorService VS CompletableFuture) (0) | 2025.02.23 |
HttpURLConnection (0) | 2024.06.07 |