3
왜 변수가 동일한 변수를 호출하는 (재귀처럼 보이는) 메서드를 호출 (자체 초기화) 할 수 있습니까? 끝없는 재귀가 예상 되나 오류없이 컴파일됩니다. 설명이 뭐야?전달 참조 및 재귀
class Forward {
static int test(){
return i;
}
static int i=test();
public static void main(String[] args) {
System.out.println(test()); //sout= 0
System.out.println(i); //sout =0
}
}
또 다른 예입니다. j
를 참조하는 것은 오류 ("불법 전방 참조")를 제공하는 동안 왜 Backwards.j
작업을 참조 않은 : 당신이 당신의 디버거에서 코드를 단계별 경우 i = test();
한 번만 지금까지 실행되는 것을 볼 수
class Backwards{
//static int i=j; //illegal forward reference;
static int i=Backwards.j; //reference through class works
static int j=i;
public static void main(String[] args) {
System.out.println(i);
System.out.println(j);
}
}
답변을 이해 했습니까? – ItamarG3