在這篇文章中,我們將重點放在方法如何與靜態和非靜態成員交互以及為什麼靜態局部變數不是允許。這是一個常見的面試主題,也是 Java 開發人員的重要概念。
這篇文章建立在本系列之前的文章中涵蓋的概念的基礎上。如果您不熟悉 static 關鍵字,我建議您先查看靜態區塊和靜態變數以獲得更好的基礎,然後再深入討論此處討論的主題。
由於靜態方法在類別層級操作,因此它們無法直接存取實例成員。
package keywords.static_keyword; public class StaticVariables { // Static and non-static variables static int idStatic = 1; // Shared across all instances int id; // Unique to each instance String name; // Unique to each instance // Static block – Runs once when the class is loaded static { displayStatic(); // Call to static method // Cannot access non-static method in static block display(); // --> Compilation error } // Constructor to initialize non-static variables public StaticVariables(String name) { this.id = ++idStatic; // Increment static ID for every new instance this.name = name; } // Non-static method: Can access both static and non-static members void display() { System.out.println("Instance Method:"); System.out.println("Static ID: " + idStatic + ", Instance ID: " + id + ", Name: " + name); } // Static method: Can only access static members directly static void displayStatic() { System.out.println("Static Method:"); System.out.println("Static ID: " + idStatic); // Static local variables are not allowed static int localVar = 10; // --> Compilation error } public static void main(String[] args) { // Call static method directly displayStatic(); // Create instances to access non-static methods StaticVariables obj1 = new StaticVariables("Object1"); StaticVariables obj2 = new StaticVariables("Object2"); // Access non-static methods through objects obj1.display(); obj2.display(); } }
Java 不允許在方法或區塊中使用靜態局部變數。
package keywords.static_keyword; public class StaticVariables { // Static and non-static variables static int idStatic = 1; // Shared across all instances int id; // Unique to each instance String name; // Unique to each instance // Static block – Runs once when the class is loaded static { displayStatic(); // Call to static method // Cannot access non-static method in static block display(); // --> Compilation error } // Constructor to initialize non-static variables public StaticVariables(String name) { this.id = ++idStatic; // Increment static ID for every new instance this.name = name; } // Non-static method: Can access both static and non-static members void display() { System.out.println("Instance Method:"); System.out.println("Static ID: " + idStatic + ", Instance ID: " + id + ", Name: " + name); } // Static method: Can only access static members directly static void displayStatic() { System.out.println("Static Method:"); System.out.println("Static ID: " + idStatic); // Static local variables are not allowed static int localVar = 10; // --> Compilation error } public static void main(String[] args) { // Call static method directly displayStatic(); // Create instances to access non-static methods StaticVariables obj1 = new StaticVariables("Object1"); StaticVariables obj2 = new StaticVariables("Object2"); // Access non-static methods through objects obj1.display(); obj2.display(); } }
static void displayStatic() { static int localVar = 10; // --> Compilation error }
Context | Access Static Members | Access Non-Static Members | Allow Static Local Variables? |
---|---|---|---|
Static Method | Yes | No | No |
Non-Static Method | Yes | Yes | No |
Static Block | Yes | No | No |
靜態方法和成員是Java中必不可少的工具。以下是主要要點:
透過理解這些規則,您將能夠在程式中有效地使用靜態方法。
Java 基礎
陣列面試重點
Java 記憶體基礎
集合架構重點
以上是Static 關鍵字:存取方法中的靜態和非靜態成員的詳細內容。更多資訊請關注PHP中文網其他相關文章!