ocajp 자격증 (Oracle Certified Associate Ja
ocjap 문제 해설 - Static 62
xemaker
2020. 3. 5. 18:04
public class Static62 {
static int count=0;
int i=0;
public void changeCount() {
while(i<5) {
i++;
count++;
}
}
public static void main(String[] args) {
Static62 check1 = new Static62();
Static62 check2 = new Static62();
check1.changeCount();
check2.changeCount();
System.out.print(check1.count + " " + check2.count);
}
}
What is the result?
A. 5 : 5
B. 10 : 10
C. 5 : 10
D. Compilation fails
정답: B
결과:
10 10
해설:
자바의 static 변수에 대해 이해하고 있는지 묻는 문제 입니다.
참고로 i 변수를 찍어보면
public class Static62 {
static int count=0;
int i=0;
public void changeCount() {
while(i<5) {
i++;
count++;
}
}
public static void main(String[] args) {
Static62 check1 = new Static62();
Static62 check2 = new Static62();
check1.changeCount();
check2.changeCount();
System.out.print(check1.count + " " + check2.count);
System.out.println();
System.out.print(check1.i + " " + check2.i);
}
}
결과:
10 10
5 5
i는 static 변수가 아니고 객체를 생성할때 메모리에 올라가는 변수이니 5가 출력됩니다.