CodeGym /Java 博客 /随机的 /Java 中的静态变量
John Squirrels
第 41 级
San Francisco

Java 中的静态变量

已在 随机的 群组中发布

什么是静态变量?

静态变量或类变量在类的所有实例之间共享,并且可以在不创建类实例的情况下访问和修改。让我们深入了解Java中 静态变量的使用和实现。

Java中的静态变量示例

静态变量最常见的用途之一是在类的所有实例之间共享。例如,跟踪创建的实例数量的计数器变量,或保存永不更改的值的CONSTANT变量。考虑一个代表银行帐户的类。以下代码片段显示了如何使用 静态变量nextAccountNumber为每个新帐户分配唯一的帐号:

例子

// BankAccount class definition
class BankAccount {
  // Declare static variable nextAccountNumber
  static int nextAccountNumber = 1001;

  // Declare instance variables for accountNumber and balance.
  int accountNumber;
  double balance;

  // BankAccount constructor that assigns unique account number to each instance
  BankAccount() {
    // Assign the current value of nextAccountNumber to accountNumber.
    accountNumber = nextAccountNumber;

    // Increment nextAccountNumber for the next BankAccount object.
    nextAccountNumber++;
  }

  // Method to get the account number of this BankAccount object.
  int getAccountNumber() {
    return accountNumber;
  }

  // Method to get the balance of this BankAccount object.
  double getBalance() {
    return balance;
  }

  // Method to deposit an amount into this BankAccount object.
  void deposit(double amount) {
    // Increase balance by the amount deposited.
    balance += amount;
  }

  // Method to withdraw an amount from this BankAccount object.
  void withdraw(double amount) {
    // Decrease balance by the amount withdrawn.
    balance -= amount;
  }
}

// Main method definition
class Main {
  static void main(String[] args) {
    // Create three BankAccount objects: account1, account2, and account3.
    BankAccount account1 = new BankAccount();
    BankAccount account2 = new BankAccount();
    BankAccount account3 = new BankAccount();

    // Display the value of the static variable nextAccountNumber after creating each BankAccount object.
    System.out.println("Value of nextAccountNumber after creating account1: " + account1.getAccountNumber());
    System.out.println("Value of nextAccountNumber after creating account2: " + account2.getAccountNumber());
    System.out.println("Value of nextAccountNumber after creating account3: " + account3.getAccountNumber());
  }
}

输出

创建 account1 后 nextAccountNumber 的值: 1001 创建 account2 后 nextAccountNumber 的值: 1002 创建 account3 后 nextAccountNumber 的值: 1003
在此示例中,main 方法创建BankAccount类的三个实例。BankAccount构造函数将nextAccountNumber分配给新帐户,并将nextAccountNumber变量加 1。getAccountNumber方法返回每个帐户帐号。然后,main 方法打印出每个帐户的帐号和nextAccountNumber变量。您可以看到每个帐号都是唯一的,并且对于每个创建的新帐户, nextAccountNumber变量都会增加 1。 静态变量还可以用于实现单例设计模式,其中只能创建一个类的一个实例。单例类是一种在任何Java应用程序中只能有一个实例的类,同时提供对此实例的全局访问点。以下代码片段显示了如何使用 静态变量实例来实现单例类:

例子

public class Singleton {
    // using static variable in the singleton class
    private static Singleton instance;
    private Singleton() { }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    public void displayMessage() {
        System.out.println("This is a singleton class");
    }

    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        singleton1.displayMessage();

        Singleton singleton2 = Singleton.getInstance();
        singleton2.displayMessage();

        Singleton singleton3 = Singleton.getInstance();
        singleton3.displayMessage();
    }
}

输出

这是一个单例类 这是一个单例类 这是一个单例类
在此示例中,main方法通过调用getInstance()方法创建Singleton类的三个实例。getInstance ()方法返回类的实例,如果不存在则创建一个新实例。displayMessage方法只是打印一条消息。然后main方法为每个实例调用displayMessage方法。正如您所看到的,即使我们创建了三个实例,也只创建了一个对象并在所有实例之间共享。它证明只有该类的一个实例被创建并在所有实例之间共享,并且静态变量“instance”被保存。请务必记住,静态变量的值在类的所有实例之间共享,因此一个实例对变量所做的更改将对所有其他实例可见。应仔细考虑静态变量,以避免代码中潜在的冲突和错误。例如,不建议使用静态变量来保存特定于类实例的值(例如帐户余额)。 为了巩固您所学到的知识,我们建议您观看我们的 Java 课程中的视频课程

结论

总而言之, Java 中的静态变量允许在类的所有实例之间共享数据。它们可用于在所有实例之间共享的变量,例如计数器和常量,或用于实现单例设计模式。但是,请务必记住,静态变量的值在类的所有实例之间共享,因此一个实例对变量所做的更改将对所有其他实例可见。
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION