변수의 스코프(scope)란

무엇인가요?

변수의 스코프(scope)

변수의 스코프란, 변수의 활동영역입니다.

실생활 비유

서울 사는 철수와, 부산 사는 철수는 다릅니다.

코드 예

실행 코드

  1. public class VariableScope {
  2. public static void main(String[] args) {
  3. int x = 3;
  4. System.out.printf("x = %d in main()\n", x);
  5. foo(x);
  6. System.out.printf("x = %d in main()\n", x);
  7. }
  8. public static void foo(int x) {
  9. x = x * x;
  10. System.out.printf("x = %d in foo()\n", x);
  11. }
  12. }

실행 결과

  1. x = 3 in main()
  2. x = 9 in foo()
  3. x = 3 in main()
[Markdown Preview]