歡迎回到Java 關鍵字基礎系列!這篇文章主要是關於靜態變數——Java 中的一個強大功能,可讓您在同一類別的多個物件之間共享資料。
我們將透過實踐範例和見解來介紹靜態變數的目的、行為和關鍵用例。在單獨的貼文中,我們將深入研究靜態方法以避免過多的內容讓您不知所措。
這篇文章建立在先前文章中討論的概念的基礎上。我建議查看最終關鍵字和靜態區塊,以更深入地了解此處涵蓋的主題。
靜態變數屬於類別而不是單一實例。它在類別的所有物件之間共享,並且在這些物件之間保留相同的值。
package keywords.static_keyword; public class StaticVariables { // Static variable: Shared across all instances of the class // Automatically initialized to default value on class loading static int idCounter; // int default value -> 0 // Static final variables // Must be initialized at declaration or in a static block static final String COMPANY_NAME = "TechCorp"; static final String OFFICE_CODE; // Instance variables: Unique to each object int employeeId; String employeeName; // Static final variable Initialized in a static block static { // Default region: US String region = System.getProperty("user.region", "US"); switch (region) { case "EU": regionalOfficeCode = "EU-01"; break; case "APAC": regionalOfficeCode = "AP-11"; break; default: regionalOfficeCode = "US-00"; } System.out.println("Static Block Invoked: Office Code set to " + regionalOfficeCode); } // Constructor: Assigns a unique ID to each object public StaticVariables(String name) { this.employeeName = name; this.employeeId = ++idCounter; // Incrementing the shared counter } // Instance method // Displays instance details along with shared data(static variables) void displayEmployeeDetails() { System.out.println("Employee ID: " + employeeId + ", Name: " + employeeName + ", Company: " + COMPANY_NAME + ", Office Code: " + OFFICE_CODE); } public static void main(String[] args) { // Creating instances to observe static variable behavior StaticVariables emp1 = new StaticVariables("Alice"); StaticVariables emp2 = new StaticVariables("Bob"); emp1.displayEmployeeDetails(); emp2.displayEmployeeDetails(); // Accessing the static variable directly using the class name System.out.println("Total Employees: " + StaticVariables.idCounter); } }
Static Block Invoked: Office Code set to US-00 Employee ID: 1, Name: Alice, Company: TechCorp, Office Code: US-00 Employee ID: 2, Name: Bob, Company: TechCorp, Office Code: US-00 Total Employees: 2
靜態變數:
靜態最終變數:
靜態區塊:
實例變數與方法:
靜態變數的類別級存取:
package keywords.static_keyword; public class StaticVariables { // Static variable: Shared across all instances of the class // Automatically initialized to default value on class loading static int idCounter; // int default value -> 0 // Static final variables // Must be initialized at declaration or in a static block static final String COMPANY_NAME = "TechCorp"; static final String OFFICE_CODE; // Instance variables: Unique to each object int employeeId; String employeeName; // Static final variable Initialized in a static block static { // Default region: US String region = System.getProperty("user.region", "US"); switch (region) { case "EU": regionalOfficeCode = "EU-01"; break; case "APAC": regionalOfficeCode = "AP-11"; break; default: regionalOfficeCode = "US-00"; } System.out.println("Static Block Invoked: Office Code set to " + regionalOfficeCode); } // Constructor: Assigns a unique ID to each object public StaticVariables(String name) { this.employeeName = name; this.employeeId = ++idCounter; // Incrementing the shared counter } // Instance method // Displays instance details along with shared data(static variables) void displayEmployeeDetails() { System.out.println("Employee ID: " + employeeId + ", Name: " + employeeName + ", Company: " + COMPANY_NAME + ", Office Code: " + OFFICE_CODE); } public static void main(String[] args) { // Creating instances to observe static variable behavior StaticVariables emp1 = new StaticVariables("Alice"); StaticVariables emp2 = new StaticVariables("Bob"); emp1.displayEmployeeDetails(); emp2.displayEmployeeDetails(); // Accessing the static variable directly using the class name System.out.println("Total Employees: " + StaticVariables.idCounter); } }
在這篇文章中,我們探討了靜態變數-一個支援跨實例共享狀態的基本功能。了解靜態變數有助於編寫更有效率的程式碼,特別是在管理需要在多個物件之間保持一致的資料時。
在下一篇文章中,我們將深入研究靜態方法,探索它們的行為、限制和最佳實踐。
Java 基礎
陣列面試重點
Java 記憶體基礎
集合架構重點
以上是Static 關鍵字:解碼 Java 中的靜態變數的詳細內容。更多資訊請關注PHP中文網其他相關文章!