2013년 6월 27일 목요일

(130627) 4일차 StaticTest.java (static,final 테스트 )

 - 소스
public class StaticTest {

int money = 300;

static int wheelnum = 4; 

public static void main(String[] args) {
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();

st1.money = 200;
System.out.println(st1.money);
st2.money = 100;
System.out.println(st2.money);
st1.wheelnum = 5;
System.out.println(st2.wheelnum); // 공유하기 때문에 st1과 st2가 같은 곳을 가리키고 있으므로 5가 출력
System.out.println(wheelnum); // static이 붙으면 객체를 생성하지않고 접근 가능

final int value = 0;
// Modifier (변수, 메소드, 클래스) 앞에 온다.
// 변수: final variable 상수(변경 불가능)
// 메소드: final method overriding 불가
// 클래스: final class subclassing 불가. 다른 class에서 final class 상속 불가
// value = 10;
System.out.println(value);
}
}


 - 결과