728x90
반응형
[Error Note] 11. static 변수 오류-
Cannot make a static reference to the non-static field
함께 보면 좋을 글: https://whatsthatsound.tistory.com/62
class의 이름으로 호출 할 수 있어, 클래스 변수라고도 불리는 static 변수.
static 변수를 사용해 NumberPrinter.waitNumber를 출력하던 중 에러가 발생했다.
에러 메세지는 다음과 같다.
Cannot make a static reference to the non-static field
static 변수가 아닌 것은 static 메서드에 참조를 할 수 없다는 뜻이다. (멤버 변수는 사용이 불가능하다.)
이는 컴파일 순서와 관련된 오류로,
static 변수가 아닌 경우, 호출 시점에서 아직 정의되지 않았기 때문에
호출해도 출력할 수 없다! (고로 static으로 정의해야한다.)
다시 돌아가, 호출하려던 변수 waitNumber을 살펴보자.
자세히 살펴보니 watiNumber 변수는 static 변수가 아닌 것을 알 수 있다.
static 변수로 고쳐보면 어떨까?
static 변수로 변경한 뒤, 이전의 오류가 말끔히 사라진 것을 볼 수 있다!
728x90
반응형