//static => 메모리에 바로 로딩
//non-static => 객체생성 후에 로딩
public class StaticEx {
String name = "kim"; //non-static member
static int n = 20; //static member
public void print(){
System.out.println(name);
System.out.println(n);
}
public static void main(String[] args) {
int a = 10;
double b = 20.5;
System.out.println(a);
System.out.println(b);
System.out.println(n); //static member라서 바로 참조 가능
StaticEx e = new StaticEx();
System.out.println(e.name);
//non-static member라 객체생성 후 참조 가능
}
}
static의 사전적인 뜻은 정적인,정지된 이라는 뜻이다.
static 변수들은 은 메모리에 바로 로딩이 되기때문에 StaticEx e = new StaticEx();를 해서 객체 생성을 해줄 필요 없이 바로 참조가 가능하다
우선 정수를 담은 n은 static이다 name은 non-static변수이다.
그럼 밑의 main메소드를 보자 변수 a 와 b 가 있다 이 둘은 앞에 static 이 없으므로 non-static변수들이다.
출력을 위해 System.out.println을 써보자
a와,b는 main메소드 안의 지역변수이므로 바로 참조가 가능하다
n은 위의 StaticEx의 전역변수이다 하지만 static멤버이다 그래서 바로 참조가 가능하다
name은 non-static멤버이다 즉 StaticEx e = new StaticEx();를 통해 객체를 생성해서 메모리에 올려줘야 참조가 가능하다
static에서 기억할 것은 두개밖에없다
static은 메모리에 바로 로딩 > main메소드에서 객체 생성 없이 참조 가능
non-static은 메모리에 바로 로딩 되지 않음 >main메소드에서 객체 생생 후 참조 가능