v1이 가진 globalScope에 10을 넣고, v2가 가진 globalScope에는 20을 넣었습니다. 그 후에 각각 값들을 출력했습니다. 전체 코드는 다음과 같습니다. 자, 실행해볼까요?
package javaStudy;
public class VariableScopeExam {
int globalScope = 10; --- ①
static int staticVal = 7; --- ②
public void scopeTest(int value) {
int localScope = 20;
System.out.println(globalScope);
System.out.println(localScope);
System.out.println(value);
}
public void scopeTest2(int value2) {
System.out.println(globalScope);
// System.out.println(localScope);
// System.out.println(value);
System.out.println(value2);
}
public static void main(String[] args) {
System.out.println(staticVal); --- ③
VariableScopeExam v1 = new VariableScopeExam();
System.out.println(v1.globalScope); --- ④
VariableScopeExam v2 = new VariableScopeExam();
v1.globalScope = 10;
v2.globalScope = 20;
System.out.println(v1.globalScope); --- ⑤
System.out.println(v2.globalScope); --- ⑥
}
}
실행결과
7
10
10
20
③~⑥까지 출력됐습니다.
③ ②에서 선언한 static한 int형 변수 staticVal을 실행했습니다.
④ ①을 보면 globalScope에 원래 10이 들어있었기 때문에 10을 출력했습니다.
⑤ v1의 globalScope에 10이란 값을 넣었기 때문에 10을 출력했습니다.
⑥ v2의 globalScope에 20이란 값을 넣었기 때문에 20을 출력했습니다.