在Java中,我們能否對非靜態欄位進行靜態引用?
Java類包含三種變數:靜態(類)變數、例項變數和區域性變數。
區域性變數 − 這些變數屬於並宣告/定義在方法/塊/構造器內。這些變數的作用域僅限於方法(或塊或構造器),並在其執行完畢後被銷燬。
例項變數 − 這些變數屬於類的例項(物件)。它們在類內但方法外宣告。在類例項化時初始化。可以從該類的任何方法、構造器或塊中訪問它們。
必須使用物件訪問例項變數。即,要訪問例項變數,需要建立類的物件,並使用此物件訪問這些變數。
類/靜態變數 − 類/靜態變數屬於類,與例項變數一樣,它們在類內、任何方法之外宣告,但使用`static`關鍵字。
它們在編譯時可用,可以在例項化類之前/無需例項化類即可訪問它們,在整個類中只有一個靜態欄位的副本可用,即靜態欄位的值在所有物件中都相同。可以使用`static`關鍵字定義靜態欄位。
對非靜態變數的靜態引用
如上所述,靜態變數使用類名引用(訪問)。
System.out.println(MyClass.data);
即,使用靜態引用引用變數意味著使用類名引用。
但是,要訪問例項變數,必須建立一個物件,這些變數在例項化之前在記憶體中不可用。
因此,不能在Java中對非靜態欄位(變數)進行靜態引用。如果仍然嘗試這樣做,則會生成一個編譯時錯誤,提示“無法從靜態上下文中引用非靜態變數math”。
示例
下面的Java程式接受使用者輸入的分數,並判斷他是否及格。
在這裡,我們在靜態方法`wasPromroted()`中直接訪問例項變數(只需指定它們的名稱,就像使用靜態變數一樣)。由於不允許這樣做,這將生成編譯時錯誤。
import java.util.Scanner; public class StudentMarks { Scanner scan1 = new Scanner(System.in); private double math; private double science; private double english; public StudentMarks(double math, double science, double english) { this.math = math; this.science = science; this.english = english; } public static boolean wasPromroted(StudentMarks marks) { if(math>=85 && science>=75 && english>=65) { return true; } return false; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter your math score: "); double math = sc.nextDouble(); System.out.println("Enter your science score: "); double science = sc.nextDouble(); System.out.println("Enter your english score: "); double english = sc.nextDouble(); StudentMarks marks = new StudentMarks(math, science, english); boolean bool = wasPromroted(marks); if(bool) { System.out.println("Congratulations you've got promoted"); } else { System.out.println("Sorry try again"); } } }
輸出
StudentMarks.java:16: error: non-static variable math cannot be referenced from a static context if(math>=85 && science>=75 && english>=65) ^ StudentMarks.java:16: error: non-static variable science cannot be referenced from a static context if(math>=85 && science>=75 && english>=65) ^ StudentMarks.java:16: error: non-static variable english cannot be referenced from a static context if(math>=85 && science>=75 && english>=65) ^ 3 errors
解決方案
要使此程式正常工作,要麼需要將例項變數宣告為靜態,要麼應該在方法中使用物件引用它們。
import java.util.Scanner; public class StudentMarks { Scanner scan1 = new Scanner(System.in); private double math; private double science; private double english; public StudentMarks(double math, double science, double english) { this.math = math; this.science = science; this.english = english; } public static boolean wasPromroted(StudentMarks marks) { if(marks.math>=85 && marks.science>=75 && marks.english>=65) return true; return false; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter your math score: "); double math = sc.nextDouble(); System.out.println("Enter your science score: "); double science = sc.nextDouble(); System.out.println("Enter your english score: "); double english = sc.nextDouble(); StudentMarks marks = new StudentMarks(math, science, english); boolean bool = wasPromroted(marks); if(bool) { System.out.println("Congratulations you've got promoted"); } else { System.out.println("Sorry try again"); } } }
輸出
Enter your math score: 89 Enter your science score: 85 Enter your english score: 86 Congratulations you've got promoted
廣告