Cannot make a static reference to the non-static method

tttkim·2022년 1월 24일

Java

목록 보기
1/1

컴파일 순서가 객체의 멤버에 따라 다르기 때문에 발생하는 오류이다. 가장 흔한 경우는 main 메서드가 컴파일되는 시점에 정의되지 않은 메서드나 필드를 사용할 때이다.

해결법 1 : 해당 메서드나 필드를 static으로 변경한다.

public class Test5 {
	static void findStone() { // 해당 메서드를 static으로 변경
			return;
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		findStone();
	}
}

해결법 2 : main을 포함한 클래스 자신을 동적으로 생성하여 해당 인스턴스를 통해 접근한다.

public class Test5 {
	void findStone() {
			return;
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		Test5 t5 = new Test5(); // 자기자신의 객체를 동적으로 생성
		findStone();
	}
}
profile
Daily Struggling

0개의 댓글