简体   繁体   English

为给定类的所有实例存储值

[英]Storing a value for all the instances of a given class

I have a class called Foo which in some of its methods relies on a value globalTime that depends on time. 我有一个名为Foo的类,该类在某些方法中依赖于依赖于时间的globalTime值。 To simplify it let's say it's a long integer storing the number of seconds since the program started. 为简化起见,假设它是一个长整数,用于存储自程序启动以来的秒数。 Now, I want all the instances of Foo to share the exact same value for globalTime . 现在,我希望Foo的所有实例都为globalTime共享完全相同的值。 I, of course could create another class which stores the value and make all the instances of Foo retrieve the value for that class but I was just wondering: Is there a way I could do this from within the class? 我当然可以创建另一个存储该值的类,并使Foo的所有实例检索该类的值,但我只是想知道:是否可以从该类内部做到这一点?

I'm looking for something like an static field but being able to change its value, although it's stored only once, not once for every instance, and thus every instance from that class would share the same value for that field. 我正在寻找类似静态字段的内容,但是能够更改其值,尽管它只存储一次,而不是为每个实例存储一次,因此该类的每​​个实例都将为该字段共享相同的值。

I'm just curious about whether something like this exists in Java, I'm not saying it's a better or worse practice than any other alternative, so please do not discuss the "purpose" of wanting to do this. 我只是对Java中是否存在类似的东西感到好奇,我并不是说这是比其他选择更好或更坏的做法,所以请不要讨论想要这样做的“目的”。

As you have already been told to make a static field. 正如您已经被告知要创建一个静态字段。 The answer is right. 答案是正确的。

Note these things also to clear it: 请注意以下事项以清除它:

  1. static fields are associated with the classes. static字段与类相关联。 Creating or destroying objects does not have any impact on static field. 创建或销毁对象对静态字段没有任何影响。
  2. A static field remains for the scope of the class not the object. static字段保留在类的范围内,而不是对象。
  3. static fields are accessed using ClassName.fieldName . 静态字段使用ClassName.fieldName访问。

that is why creating object does not have any impact on it. 这就是为什么创建对象对其没有任何影响的原因。

You should use a static field for that. 您应该为此使用静态字段。 Value of static fields can very well be changed just as the value of usual fields. 静态字段的值可以像通常字段的值一样很好地更改。

如果在多线程中使用globalTime ,请考虑使用静态AtomicLong ,它将处理同步。

If your value depends on globalTime, use static volatile field as: 如果您的值取决于globalTime,则将static volatile字段用作:

public class Foo {
    static volatile long globalTime=System.nanoTime();

}
class B{
    public static void main(String args[]){
        System.out.println(Foo.globalTime);
    }
}

Since volatile fields avoid compiler caching the values which depends on time fields. 由于易失性字段避免了编译器缓存依赖于时间字段的值。 This SO answer would further help you. 这样的答案将进一步为您提供帮助。

Could have it as a static field wrapped around a thread that just keeps calling System.nanoTime() . 可以将它作为包裹在一直调用System.nanoTime()的线程周围的静态字段。 I don't see a problem with that. 我认为没有问题。 Something like this 像这样

    private static long timer = 0;
static {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                timer = System.nanoTime() - timer;
            }
        }
    }).start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM