public class Ex02 {
public static void main(String[] args){
System.out.println(Test2.a);
Test2.a = 20;
System.out.println(Test2.a);
System.out.println(Test2.PI);
// Test2.PI = 3.5; 컴오류. 값을 변경할 수 없음.
}
}
class Test2 {
public static int a = 10;
// 한번만 초기화 되며 값을 변경할 수 없음. 파이널 변수는 보통 대문자로 만든다.
public final stitic double PI = 3.141592;
public final static int x;
static { // statix 블럭에서 final static 변수 초기화
x = 5;
}
final int b = 10;
final int c;
public Test2() {
// x= 10; final static 변수는 생성자에서 초기화 불가.
c = 10; // final 인스턴스 변수는 생성자에서 단 한번 초기화 가능.
}
public void sub() {
// c = 5; 컴오류. instance final 변수의 값은 일반 메소드에서 변경 불가
}
}
'쌍용강북교육센터 > 7월' 카테고리의 다른 글
0716_Ex04_Calendar (0) | 2021.07.19 |
---|---|
0716_Ex03_singleton 패턴 (0) | 2021.07.19 |
0716_Ex01 : 초기화 블럭 (0) | 2021.07.18 |
0715_Ex09_this (0) | 2021.07.18 |
0715_Ex08_this (0) | 2021.07.18 |