• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware question

 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. There is a question in enthuware which asked "Which of these statements are true?" .One of the correct answers is :
Each object of a class has its own copy of each non-static member variable.


But i don't understand exactly meaning of this sentence. please explain it for me
 
Akshit dhar
Greenhorn
Posts: 17
Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Refer the attachment for answer.
Untitled.png
[Thumbnail for Untitled.png]
Answer
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:But i don't understand exactly meaning of this sentence.

That statement explains the difference between class (static) variables and instance (non-static) variables. A class (static) variable is associated with the class, rather than with any object (instance). Only one copy of this class (static) variable exists and every instance of the class shares a class variable. Any object (instance) can change the value of a class (static) variable, but you can also manipulate class (static) variables without creating an instance of the class.
It's a different story for instance (non-static) variables. Each object (instance) has its own distinct copies of the instance (non-static) variables. So if you change the value of an instance (non-static) variable of one object, it will not affect the value of that instance (non-static) variable of all other objects of that class.

All the above is illustrated in this code snippetWhen you execute the Animal class, the following output is printed:
a1: 50 Tom
a2: 25 Jerry
a1: 10 Spike
a2: 10 Jerry

So you notice if you change the instance (non-static) variable name of object a1, it has no effect on the instance (non-static) variable name of object a2. But if you change the class (static) variable max using the reference variable a2, this change is also reflected when you print the value of the class (static) variable max using the reference variable a1 (because there's only one copy of the class (static) variable max).

Hope it helps!
Kind regards,
Roel
 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends Now it is really clear to me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic