많이 헷갈리는 Static 정리해보자!
JAVA에서 Static
은 프로그램 시작 시 메모리에 할당되어 프로그램이 종료될 때 까지 사용이 되는 것을 의미하는 것으로, 모든 객체가 공유하는 메모리라는 장점이 있지만, 자주 사용될 경우 지속적인 메모리 할당으로 과부하를 줄 수 있다.
public class MainClass {
public static void main(String[] args) {
ExampleStatic.name = "Test";
ExampleStatic example = new ExampleStatic();
example.name = "Test2"; // 지향하지 않는 방식
}
}
public class ExampleStatic {
public static String name = "Example"
}
Math.max()
와 같이 java.util.Math가 대표적인 유형이다.import java.util.Math
public class ExamplePrint {
private static String name = "Eu";
private String name2 = "Gene";
public static void printMax(int x, int y) {
System.out.println(Math.max(x,y));
}
public static void printName(){
System.out.println(name);
// System.out.println(name2); // 불가능한 호출
}
}
Ref. MangKyu's Diary