개요
- 전역 변수보다 지역변수가 우선순위가 더 높다
- 메소드 내에서의 i 는 외부에 영향을 주지 않는다
Java
Copy
public class ScopeDemo {
static void a() {
int i = 0;
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
a(); // 문맥상 i 를 0으로 초기화 시키는 것처럼 보이지만 클래스 멤버 메소드 내에서만 사용되고 폐기된다.
System.out.println(i);
}
}
}
Loading
- 멤버변수 i를 선언하여 다시 수행하면 결과는 아래와 같다.
Java
Copy
public class DemoScope {
static int i;
static void a() {
i = 0;
}
public static void main(String[] args) {
for(i = 0; i < 5; i++) {
a();
System.out.println(i);
}
}
}
Loading
편집자 에어컨 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.