Static variables in java

北战南征 提交于 2021-01-28 23:09:54

问题


I have scenario like this

public class Test{

   static int a;
   int b;

   public static void main(String[] args){

      Test t1  = new Test();
      Test t2 = new Test();

   }
}

What will be the variables in object t1 and object t2?

As per my understanding since variable a is a static variable it will be both in object 1 and object 2.

And b will be created separate copy for both the objects.

But when I assign a value to variable b ie(int b=1) and call it like System.out.println(t1.b), System.out.println(t2.b)

Instead of getting an error I am getting 1 as output from both the objects.

Why is that?


回答1:


As per my understanding since variable a is static variable it will be both in object 1 and object 2.

No. It's not in either object. It exists without reference to any particular instance at all. Although Java allows you to refer to it "via" a reference, e.g. int x = t1.a; you shouldn't do so. You should instead refer to it via the class name (test.a in your case - although you should also start following Java naming conventions) to make it clear that it's static.

And b will be created separate copy for both the objects.

Yes.

But when I assign a value to variable b ie(int b=1) and call it like System.out.println(t1.b), System.out.println(t2.b) Instead of getting an error I am getting 1 as output from both the objects.

Well that's because you've basically given it an initial value to assign for each new object. That's entirely reasonable. There are still two independent variables:

t1.b = 2;
t2.b = 3;
System.out.println(t1.b); // Prints 2
System.out.println(t2.b); // Prints 3



回答2:


No, a static variable is not an instance variable but a class variable.

That's, every instance of this class shares the same reference of those variables.

Take a look at Understanding Class Members




回答3:


Here a is static variable and b is instance or non-static variable.

For static variable, only one copy is created for all objects, where as for instance variable, one copy is created for each object. Here when b=1, for every object of that Test class, one copy is created and it can access it through its object name. So output will be 1.




回答4:


Static variables are class members that are shared among all objects. There is only one copy of them in main memory. Static variables are created at run time on Heap area.

In case of yours...

int b = 1; 

it's an assignment at class level, making 1 the default value for variable b(In normal case default value is 0). hence when you print it, its gives you the ans as 1 not error.

int b = 1; //initialized b to 1 for all obj of class Test
System.out.println(t1.b); // Prints 1
System.out.println(t2.b); // Prints 1



回答5:


Of course that is exactly what it should output. If you assign say,

int b=1; 

that assignment is at class level, making 1 the default value for variable b. However, if you assign like this:

t1.b=1;

that will assign 1 to only the copy of variable b in object t1.

Try it.



来源:https://stackoverflow.com/questions/33568399/static-variables-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!