Java/Constructor (생성자)

정직 멤버변수와 인스턴스 멤버 변수의 차이점

Bohemian life 2012. 4. 11. 13:44
class StaticTest{//정직 멤버변수와 인스턴스 멤버 변수의 차이점
	static int a=10; //클래스(static)에 올라감
	int b=20; //heap에 올라감
}
public class StaticTest01 {
	public static void main(String[] args){
		System.out.println("StaticTest.a->"+StaticTest.a);
		StaticTest s1 = new StaticTest();
		StaticTest s2 = new StaticTest();
		
		System.out.println("s1.a->"+StaticTest.a+"\t s2.a->" + StaticTest.a);
		System.out.println("s1.b->"+StaticTest.a+"\t s2.b->" + s2.b);
		
		//정확한 호출 방법이 아님 StaticTest.a 로 호출해야됨
		
		StaticTest.a=100;
		System.out.print("s1.a->" + StaticTest.a);
		System.out.println("\t s2.a->"+StaticTest.a);
		
		s1.b=200;
		System.out.print("s1.b->" + s2.b);
		System.out.println("\t s2.b->"+s2.b);
	}

}

StaticTest.a->10

s1.a->10  s2.a->10
s1.b->10  s2.b->20
s1.a->100  s2.a->100
s1.b->20  s2.b->20