Java의 static 정적 제어자는 클래스의 맴버(변수 or 메서드)에 적용될 수 있다.
다음과 같은 특징과 용도를 가진다.
<aside> 📎
정적 메서드(Static Method, 클래스 메서드)
</aside>
<aside> 📎
</aside>
static으로 선언된 변수 or 메서드는 객체 인스턴스가 아니라 클래스 자체에 속한다.
따라서 클래스의 모든 인스턴스가 이 변수 or 메서드를 공유한다.
public class Example {
// static 변수
public static int staticVariable = 10;
// instance 변수
int instanceVariable = 20;
// static 메소드
public static void staticMethod() {
System.out.println("This is a static method.")
}
// 일반 메소드
public void instanceMethod() {
System.out.println("This is an instance method.")
}
}
static변수는 클래스가 메모리에 로드될 때 초기화되며, 프로그램이 종료될때까지 존재한다.
이는 메모리 사용을 효율적으로 관리하는데 도움을 준다.