publicclassVariableScopeExam{intglobalScope=10;// 인스턴스 변수publicvoidscopeTest(intvalue){intlocalScope=10;// 지역 변수System.out.println(globalScope);System.out.println(localScope);System.out.println(value);}}
클래스의 속성으로 선언된 변수 globalScope의 사용 범위는 클래스 전체이다.
매개변수로 선언된 int value는 블럭 바깥에 존재하기는 하지만, 메소드 선언부에 존재하므로 사용범위는 해당 메소드 블럭 내이다.
메소드 블럭 내에서 선언된 localScope 변수의 사용범위는 메소드 블럭 내이다.
main 메소드에서 변수 사용하기
publicclassVariableScopeExam{intglobalScope=10;// 인스턴스 변수publicvoidscopeTest(intvalue){intlocalScope=10;// 지역 변수System.out.println(globalScope);System.out.println(localScope);System.out.println(value);}publicstaticvoidmain(String[]args){System.out.println(globalScope);// 오류System.out.println(localScope);// 오류System.out.println(value);// 오류}}
같은 클래스 안에 있음에도 globalScope 변수를 사용할 수 없다.
main은 static한 메소드이다. static한 메소드에서는 static하지 않은 필드를 사용할 수 없다.
static하지 않은 필드를 main 메소드에서 사용하려면 객체를 생성한 뒤 사용 가능하다.
ex) VariableScopeExam v1 = new VariableScopeExam();
static
static으로 선언된 변수는 (static한 메소드인) main 메소드에서 사용 가능
publicclassVariableScopeExam{intglobalScope=10;// 인스턴스 변수staticintstaticVal=7;// 클래스 변수publicstaticvoidmain(String[]args){System.out.println(staticVal);// 사용가능}}
static으로 선언된 변수는 공유된다.
static으로 선언된 변수는 값을 저장할 수 있는 공간이 하나만 생성된다. 그러므로, 인스턴스가 여러 개 생성되어도 static 변수는 하나다.
VariableScopeExamv1=newVariableScopeExam();VariableScopeExamv2=newVariableScopeExam();v1.globalScope=20;// 인스턴스 변수v2.globalScope=30;System.out.println(v1.globalScope);// 20 출력System.out.println(v2.globalScope);// 30 출력v1.staticVal=10;// 클래스 변수v2.staticVal=20;System.out.println(v1.staticVal);// 20 출력System.out.println(v2.staticVal);// 20 출력
globalScope같은 변수(필드)는 인스턴스가 생성될 때 생성되기 때문에, 인스턴스 변수라고 한다.