변수의 스코프(scope)란
무엇인가요?
sehongpark님의 답변
변수의 스코프(scope)
변수의 스코프란, 변수의 활동영역입니다.
실생활 비유
서울 사는 철수와, 부산 사는 철수는 다릅니다.
코드 예
실행 코드
public class VariableScope {
public static void main(String[] args) {
int x = 3;
System.out.printf("x = %d in main()\n", x);
foo(x);
System.out.printf("x = %d in main()\n", x);
}
public static void foo(int x) {
x = x * x;
System.out.printf("x = %d in foo()\n", x);
}
}
실행 결과
x = 3 in main()
x = 9 in foo()
x = 3 in main()