欢迎回到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中文网其他相关文章!