Why output is changed only for t2.y, but not for t2.x? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why output is changed only for t2.y, but not for t2.x?

public class Test { int x=10; static int y=20; public static void main(String[] args) { Test t1=new Test(); t1.x=888; t1.y=999; Test t2=new Test(); System.out.println(t2.x+"---"+t2.y);//10---999 } } //instance variable /* If the value of a variable is varied from object to object such type of variables are called instance variables. For every object a separate copy of instance variables will be created. */ //static variable /* If the value of a variable is not varied from object to object such type variables is not recommended to declare as instance variables. We have to declare such type of variables at class level by using static modifier. In the case of instance variables for every object a separate copy will be created but in the case of static variables for entire class only one copy will be created and shared by every object of that class. */

27th Jan 2022, 1:07 AM
Minhaj Haider
Minhaj Haider - avatar
9 Answers
+ 9
... because Test.y is declared as static, i.e. y is an attribute of Test which value persists across instances. Creating a new instance of class Test will not create a new instance of attribute y. On the other hand, instances t1 and t2 each have their own attribute x, which is different from each other.
27th Jan 2022, 1:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Minhaj Haider(MiDer), all newly created instances of a static class will access the same static variable y at the time you assign values to it, and assigning a value to the variable x will always create a new address to store the value.
27th Jan 2022, 1:58 AM
Solo
Solo - avatar
+ 2
Minhaj Haider(MiDer), let's go in order. First, your code does not need to access the static variable y through the newly created instances of the class. Secondly, when you assign a value to the static variable y from a newly created instance of the static class, you are accessing the same target of the static variable of the root class. And finally, when you assign a value to the variable x from the newly created instance of the class, you create a new address where the new value will be stored. t1.x != t2.x && t1.y == t2.y // true
27th Jan 2022, 2:37 AM
Solo
Solo - avatar
+ 2
Minhaj Haider(MiDer), sorry, I have edited my answers. I saw the name of the Test class and automatically thought that you simply didn’t write the first line with the main class in the question, that is, for me it should have been like this ☺️: public class Program { public static class Test { int x=10; static int y=20; public static void main(String[] args) { Test t1=new Test(); t1.x=888; t1.y=999; Test t2=new Test(); System.out.println(t2.x+"---"+t2.y);//10---999 } } } Otherwise, why change the name of the class.☺️ Well, okay, it basically does not change the essence. I hope you already know what a global and local variable is, so a static class variable is essentially a global variable of this class and you don’t need to refer to it through the class, you shouldn’t mislead yourself and everyone else when referring to it as a local variable t1.x, just write y=9.
27th Jan 2022, 7:08 PM
Solo
Solo - avatar
+ 2
Minhaj Haider(MiDer), and this is all because, in my opinion, you basically wrote the Test class incorrectly by including the main () method in it. Basically it should look like this: public class Test { int x=10; static int y=20; } public class Program { public static void main(String[] args) { Test t1=new Test(); t1.x=888; t1.y=999; Test t2=new Test(); System.out.println(t2.x+"---"+t2.y);//10---999 } } Now you can access the static variable y only through its class, or newly created class instances, for example: Test.y=9;
27th Jan 2022, 7:17 PM
Solo
Solo - avatar
+ 1
Hatsy Rei please explain clearly and slowly
27th Jan 2022, 1:25 AM
Minhaj Haider
Minhaj Haider - avatar
+ 1
Solo My code is working and showing the output as stated above.... What you wrote in secondly paragraph, please explain clearly....again in different way....
27th Jan 2022, 9:55 AM
Minhaj Haider
Minhaj Haider - avatar
+ 1
simply put static properties are accessed globally from any object or from the class itself
27th Jan 2022, 1:15 PM
Aybak3k
Aybak3k - avatar
0
Solo , sorry, that's not i want, that's not about static classes, this is about... why t2.y value is 999, why not 20, however t2.x is 10 and not 888. I'm not getting it properly.
27th Jan 2022, 2:12 AM
Minhaj Haider
Minhaj Haider - avatar