简体   繁体   English

可以在Java中将实例变量声明为静态变量

[英]Can instance variable be declared as static variable in java

I wrote the following code: 我写了以下代码:

public class Count
{

    private static int[] array;

    public Count( int[] array )
    {
        this.array = array;
    }

    public int count()
    {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            int x = array[i];
            if (x < 0) {
                if (x == -1 && i > 0 && array[i - 1] == -1) {
                    break;
                }
            } else {
                count++;
            }
        }
        return count;
    }

}

and then in another class I wrote: 然后在另一堂课中,我写道:

Count c = new Count(new int[]{1,-1,-1});

and checked the result of c.count() . 并检查了c.count()的结果。 I didn't get a compiler error,and I got 1 as I wanted. 我没有遇到编译器错误,但我却想得到1 I wonder why because how can I assign a static variable to an instance variable? 我不知道为什么,因为如何将静态变量分配给实例变量? And also, what happens if I create another instance of class Count? 而且,如果我创建另一个Count类的实例怎么办? Will they both have the same variable?or just their own copy of it? 它们是否都具有相同的变量?或者只是它们自己的副本? and what should happen if I try to access the variable by writing Count.array ? 如果我尝试通过编写Count.array来访问变量, Count.array怎么Count.array Thanks. 谢谢。

Classes can access their own static members. 类可以访问自己的静态成员。

All classes will see the same static members (ignoring thread contention complications). 所有类都将看到相同的静态成员(忽略线程争用的复杂性)。

If you create another instance of Count they will both be modifying the same variable. 如果创建另一个Count实例,则它们都将修改同一个变量。

That's what static means... 那就是static意思...

array is shared between all the instances of Count - they can all read from it and write to it. arrayCount所有实例之间共享-它们都可以从中读取和写入。 And as you stated, if you instance modifies it, all the other instances will see the newly modified value. 如您所说,如果您实例对其进行修改,则所有其他实例将看到新修改的值。

I'd recommend that you write the example you're asking about and see. 我建议您编写您要询问并查看的示例。 There's no better authority than the JVM, and it'll tell you faster than SO will. 没有比JVM更好的权限了,它会比SO更快地告诉您。

I think you should understand the meaning of class and instance variables and pick the one that matches your intent. 我认为您应该了解类和实例变量的含义,并选择与您的意图匹配的变量。

So it's either class: 所以这是两个类:

public class Count
{
   // Shared by all 
   private static int[] array;
}

or instance: 或实例:

public class Count
{
   // Owned by instance
   private int[] array;

   public Count(int [] v) {
       if (v == null) throw new IllegalArgumentException("array cannot be null");
       this.array = new int(v.length);
       System.arraycopy(v, this.array, 0, v.length);
   }
}

You should not just assign the array reference to the private data member. 您不应该只是将数组引用分配给私有数据成员。 You need to allocate memory and copy the values into it. 您需要分配内存并将值复制到其中。 Otherwise the supplier of the input array will modify the private values if it updates using their reference. 否则,如果输入数组的提供者使用其引用进行更新,则其将修改私有值。

Too many people don't realize that assigning mutable references to private members breaks encapsulation. 太多的人没有意识到向私有成员分配可变引用会破坏封装。 Maybe you won't be one of them now. 也许您现在不会成为其中之一。

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

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