java 静态字段_Java中的静态字段

java 静态字段

There can be times when it's useful to have values that are shared across all instances of a particular class. Static fields and static constants enable this type of sharing by belonging to the class and not to the actual objects.

有时在特定类的所有实例之间共享值很有用。 静态字段和静态常量通过属于而不是实际对象来启用这种类型的共享。

静态修饰符 ( The Static Modifier )

Normally fields and methods defined in a class can be used only when an object of that class type has been created. For example, consider a simple Item class that keeps track of goods in a store:

通常,只有在创建了该类类型的对象后,才能使用在类中定义的字段和方法。 例如,考虑一个简单的Item类,该类跟踪商店中的商品:

 public class Item {
   private String itemName;
   public Item(String itemName)
   {
     this.itemName = itemName;
   }
   public String getItemName()
   {
     return itemName;
   }
 } 

To be able to use the getItemName() method, we must first create an Item object, in this case, catFood:

为了能够使用getItemName()方法,我们必须首先创建一个Item对象,在本例中为catFood:

 public class StaticExample {
   public static void main(String[] args) {
     Item catFood = new Item("Whiskas");
     System.out.println(catFood.getItemName());
   }
 } 

However, if the static modifier is included in a field or method declaration, no instance of the class is required in order to use the field or method — they are associated with the class and not an individual object. If you look back at the above example, you will see that the static modifier is already being used in the main method declaration:

但是,如果将静态修饰符包含在字段或方法声明中,则无需使用该类的实例即可使用该字段或方法-它们与该类相关联,而不是与单个对象相关联。 如果回头看上面的示例,您将看到在主方法声明中已经使用了static修饰符:

 public static void main(String[] args) { 

The main method is a static method that does not require an object to exist before it can be called. As main() is the starting point for any Java application, there are in fact no objects already in existence to call it. You could, if you felt like having a program that continually calls itself, do this:

main方法是一种静态方法 ,不需要调用对象就可以存在。 由于main()是任何Java应用程序的起点,因此实际上没有对象可以调用它。 如果您想拥有一个不断调用自身的程序,可以这样做:

 public class StaticExample {
   public static void main(String[] args) {
     String[] s = {"random","string"};
     StaticExample.main(s);
     }
 }

Not very useful, but notice how the main() method can be called without an instance of a StaticExample class.

并不是很有用,但是请注意如何在没有StaticExample类实例的情况下调用main()方法。

什么是静态场? ( What Is a Static Field? )

Static fields are also known as class fields. They are simply fields that have the static modifier in their declarations. For example, let's go back to the Item class and add a static field:

静态字段也称为类字段。 它们只是在声明中具有static修饰符的字段。 例如,让我们回到Item类并添加一个静态字段:

 public class Item {
   //static field uniqueId
   private static int uniqueId = 1;
   private int itemId;
   private String itemName;
   public Item(String itemName)
   {
     this.itemName = itemName;
     itemId = uniqueId;
     uniqueId++;
   }
 }

The fields itemId and itemName are normal non-static fields. When an instance of an Item class is created, these fields will have values that are held inside that object. If another Item object is created, it too will have itemId and itemName fields for storing values.

字段itemId和itemName是普通的非静态字段。 创建Item类的实例时,这些字段将具有保存在该对象内部的值。 如果创建了另一个Item对象,它也将具有用于存储值的itemId和itemName字段。

The uniqueId static field, however, holds a value that will be the same across all Item objects. If there are 100 Item objects, there will be 100 instances of the itemId and itemName fields, but only one uniqueId static field.

但是,uniqueId静态字段的值在所有Item对象中都相同。 如果有100个Item对象,则itemId和itemName字段将有100个实例,但是只有一个uniqueId静态字段。

In the above example, uniqueId is used to give each Item object a unique number. This is easy to do if every Item object that is created takes the current value in the uniqueId static field and then increments it by one. The use of a static field means that each object does not need to know about the other objects to get a unique id. This could be useful if you wanted to know the order in which the Item objects were created.

在上面的示例中,uniqueId用于为每个Item对象赋予唯一编号。 如果创建的每个Item对象都在uniqueId静态字段中采用当前值,然后将其递增1,则这样做很容易。 使用静态字段意味着每个对象无需了解其他对象即可获得唯一的ID 。 如果您想知道Item对象的创建顺序,这可能会很有用。

什么是静态常数? ( What Is a Static Constant? )

Static constants are exactly like static fields except that their values cannot be changed. In the field declaration, the final and static modifiers are both used. For example, perhaps the Item class should impose a restriction on the length of the itemName. We could create a static constant maxItemNameLength:

静态常量与静态字段完全一样,只是它们的值无法更改。 在字段声明中,将同时使用finalstatic修饰符。 例如,也许Item类应该对itemName的长度施加限制。 我们可以创建一个静态常量maxItemNameLength:

 public class Item {
   private static int id = 1;
   public static final int maxItemNameLength = 20;
   private int itemId;
   private String itemName;
   public Item(String itemName) 
   {
     if (itemName.length() > maxItemNameLength)
     {
       this.itemName = itemName.substring(0,20);
     }
     else
     {
       this.itemName = itemName;
     }
     itemId = id;
     id++;
   } } 

As with static fields, static constants are associated with the class rather than an individual object:

与静态字段一样,静态常量与类关联,而不是与单个对象关联:

 public class StaticExample {
   public static void main(String[] args) {
     Item catFood = new Item("Whiskas");
     System.out.println(catFood.getItemName());
     System.out.println(Item.maxItemNameLength);
     }
 }

There are two important things to notice about the maxItemNameLength static constant:

关于maxItemNameLength静态常量,有两点需要注意:

  • It is declared as a public field. Generally it's a bad idea to make a field public in any class you design but in this case, it doesn't matter. The value of the constant cannot be changed.

    它被宣布为公共领域。 通常,在您设计的任何类中公开一个领域都是一个坏主意,但是在这种情况下,这并不重要。 该常数的值不能更改。
  • The static constant is used from the class name Item, not an Item object.

    从类名Item使用静态常量,而不是Item对象。

Static constants can be seen throughout the Java API. For example, the integer wrapper class has two that store the maximum and minimum values an int data type can have:

在整个Java API中都可以看到静态常量。 例如, 整数包装器类有两个用于存储int 数据类型可以具有的最大值和最小值:

 System.out.println("The max value for int is: " + Integer.MAX_VALUE);
 System.out.println("The min value for int is: " + Integer.MIN_VALUE);
 Output:
 The max value for int is: 2147483647
 The min value for int is: -2147483648

翻译自: https://www.thoughtco.com/static-fields-2034338

java 静态字段

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值