中級 › non-static variable cannot be referenced の原因と直し方 › パターン5
non-static variable cannot be referenced の原因と直し方
staticとインスタンスの境界。
1 public class Main { 2 int id = 1; 3 static void show() { 4 System.out.println(_____________); ^ 5 } 6 public static void main(String[] args) { 7 show(); 8 } 9 }
Main.java:4: error: non-static variable id
cannot be referenced from a static context
空欄を埋めてビルドを通す
staticメソッドの中では、mainに限らずインスタンスの変数へ直接アクセスできません。
次の問題