minjava 2025. 7. 12. 12:05

클래스 자체에 속하게 만들기 위해서 사용한다.

객체(인스턴스)를 만들지 않고도 바로 사용할 수 있게

보통 단순 계산, 도우미 함수 등에서 자주 쓰인다.

 

static 사용한 경우 -> 객체 생성 불필요

public class Test{

public static void main(String[] args) {
run();
}

public static void run() {
System.out.println("안녕");
}

}

 

static 사용하지 않은 경우 -> 객체 생성 필요

public class Test{

public static void main(String[] args) {
Test t = new Test();
run();
}

public void run() {
System.out.println("안녕");
}

}