什么时候我会在Java中使用类变量而不是示例变量?

vwhgwdsa  于 2023-04-10  发布在  Java
关注(0)|答案(4)|浏览(86)

下面是我定义的一个泛型类,我想知道的是,当我创建更具体的类时,例如CAR类,我什么时候会使用类变量?我个人对类变量的理解是,在类中声明的类变量的单个副本将使用关键字static声明,并且已经从类示例化的每个对象将包含类变量的单个副本。
示例变量允许从类创建的类/对象的每个示例在每个对象上都有一个单独的示例变量副本。
因此,示例变量对于定义类/数据类型的属性是有用的,例如,房子会有一个位置,但是现在我什么时候在房子对象中使用类变量?或者换句话说,在设计类时,类对象的正确用法是什么?

public class InstanceVaribale {
public int id; //Instance Variable: each object of this class will have a seperate copy of this variable that will exist during the life cycle of the object.
static int count = 0; //Class Variable: each object of this class will contain a single copy of this variable which has the same value unless mutated during the lifecycle of the objects.

InstanceVaribale() {
    count++;

}
public static void main(String[] args) {

    InstanceVaribale A = new InstanceVaribale();
    System.out.println(A.count);
    InstanceVaribale B = new InstanceVaribale();
    System.out.println(B.count);
    System.out.println(A.id);
    System.out.println(A.count);
    System.out.println(B.id);
    System.out.println(B.count);    
    InstanceVaribale C = new InstanceVaribale();
    System.out.println(C.count);
}
}
qhhrdooz

qhhrdooz1#

我个人对类变量的理解是,在类中声明的类变量的单个副本将使用关键字static声明,并且从类示例化的每个对象将包含类变量的单个副本。
不。并不是说“每个对象都将包含一个副本”。静态变量与 type 相关联,而不是与该类型的每个示例相关联。instances 根本没有变量。
只有一个变量(假设你只从一个类加载器加载它)* 然而 * 有很多示例。没有示例?仍然是一个变量。一百万个示例?仍然是一个变量。
静态变量对于常量或类似常量的东西(如记录器或“有效价格集”等)非常有用。在应用程序的过程中不会改变的东西。根据我的经验,它们应该几乎总是final,类型应该是不可变的类型(像String)。在可能的情况下,对静态变量也使用不可变的集合-或者确保变量是私有的,并且永远不要在类中改变集合。
你应该避免使用静态变量来存储全局变化的状态。这会使代码更难测试和推理。

nom7f22z

nom7f22z2#

1.每一个object of the class都会有自己的示例变量副本,它的One per Object.
2.但是static variable被类的所有对象共享,它的One per Class.
**3.**现在我将给予两个例子,其中这两个将具有重要性。
示例变量:

考虑一个游戏程序,然后每个玩家将有不同的名字,分数,武器功率,达到的阶段等......

静态变量:

考虑一个银行程序,其中每个客户端将被赋予一个ID,该ID比前一个ID更大且唯一,因此静态变量将适用于此。

guz6ccqo

guz6ccqo3#

静态变量用于存储在类的所有示例之间共享的值。
如果不是这样,它应该是一个示例变量。

toiithl6

toiithl64#

定义:
**类变量:**类变量也称为静态变量,在类中使用static关键字声明,但在方法,构造函数或块之外。There would only be one copy of each class variable per class, regardless of how many objects are created from it
**示例变量:**示例变量在类中声明,但在方法外部声明。When space is allocated for an object in the heap, a slot for each instance variable value is created
**局部变量:**局部变量在方法、构造函数或块中声明。Local variables are created when the method, constructor, or block is entered and the variable will be destroyed once it exits the method, constructor, or block
示例:
类变量:

class ClassVar {
  static int count; //class level variable
  public static void main(String[] args) {

    ClassVar obj1 = new ClassVar();
    ClassVar obj2 = new ClassVar();
    obj1.count=5;
    obj2.count=5;
    
    obj1.count++; //change in object1's count

    System.out.println(obj1.count); // 6
    System.out.println(obj2.count); // 6
  }
}

示例变量:

class InstanceVar {
  int count; //instance level variable
  public static void main(String[] args) {

    InstanceVar obj1 = new InstanceVar();
    InstanceVar obj2 = new InstanceVar();
    obj1.count=5;
    obj2.count=5;
    
    obj1.count++; //change in object1's count

    System.out.println(obj1.count); // 6
    System.out.println(obj2.count); // 5
  }
}

正如您可能已经注意到的那样,带有单词static的相同代码更改了实现。

合并示例

public class Car {
    // Class variable
    static int numCars = 0;
    
    // Instance variables
    String make;
    String model;
    int year;
    
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
        
        // Increment the number of cars each time a new Car object is created
        numCars++;
    }
    
    public void printCarInfo() {
        System.out.println("Make: " + make + " ,Model: " + model + " ,Year: " + year);
    }
    
    public static void printNumCars() {
        System.out.println("Number of cars: " + numCars);
    }
}

在这个例子中,numCars是一个类变量,因为它是用static关键字声明的,这意味着它被类的所有示例共享。我们使用它来跟踪已经创建的Car对象的总数。每次创建Car类的新示例时,这个值都会增加。
makemodelyear是示例变量,因为它们没有被声明为static,这意味着它们属于每个单独的Car对象。它们将保存特定汽车示例的详细信息。
现在如果我们添加一个main方法来演示

public static void main(String[] args) {
    Car car1 = new Car("Toyota", "Camry", 2021);
    Car car2 = new Car("Honda", "Civic", 2022);
    Car car3 = new Car("Ford", "Mustang", 2023);

    car1.printCarInfo();
    car2.printCarInfo();
    car3.printCarInfo();

    Car.printNumCars();
}

这里我们创建了三个Car示例并打印了它们的详细信息。现在numCars的值也是3,因为这是一个类var,将与Car类的所有示例共享。最终输出如下所示。

Make: Toyota ,Model: Camry ,Year: 2021
Make: Honda ,Model: Civic ,Year: 2022
Make: Ford ,Model: Mustang ,Year: 2023
Number of cars: 3

相关问题