• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance Variable vs Static Variable, code tracing problem  RSS feed

 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am studying for an upcoming exam for my java programming class. I am having a hard time understanding the output of the following code segment. I would expect it to print out AB AB CD, but it does not. Can someone please help me understand why the output is what it is? I would greatly appreciate it.


Thanks in advance.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between instance variables and class variables (a static variable is a class variable) is this: each instance of the class has its own copy of the instance variables, but there is only one copy of the class variables that is shared by all the instances of the class.

So, what happens in your code?

In line 4, you create a new Foo object. The constructor in line 19 is called. It sets anInstanceVar to "A" and aStaticVar to "B". In line 5, you print the object (which ofcourse goes via the toString() in line 25) and you get "AB".

In line 6, you create another new Foo object. Again, the constructor in line 19 is called. It sets anInstanceVar to "C" and aStaticVar to "D".

Now, you print the first Foo object, that you created in line 4. You get "AD". Why? Because aStaticVar is a class variable. There is only one copy of this variable which is shared by all Foo instances. So for both the Foo objects, you'll now see the value "D" for aStaticVar.

Then, in line 8, you print the second Foo object, which will show "CD".

For a detailed description, see: Understanding Instance and Class Members in Oracle's Java Tutorials.
 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
So, what happens in your code?

In line 4, you create a new Foo object. The constructor in line 19 is called. It sets anInstanceVar to "A" and aStaticVar to "B". In line 5, you print the object (which ofcourse goes via the toString() in line 25) and you get "AB".

In line 6, you create another new Foo object. Again, the constructor in line 19 is called. It sets anInstanceVar to "C" and aStaticVar to "D".

Now, you print the first Foo object, that you created in line 4. You get "AD". Why? Because aStaticVar is a class variable. There is only one copy of this variable which is shared by all Foo instances. So for both the Foo objects, you'll now see the value "D" for aStaticVar.


Thank you very much man, I finally understand this. I cant tell you how much I appreciate this explanation.

So basically if 10 new Foo objects where created containing different characters... After creating all objects, if we decided to print all of the objects out. They would all share the same Static variable (which is the static variable of the LAST foo object created ) and they would not neccessarily share the same instance variables.

Correct?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!