Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Enthuware question

 
Ranch Hand
Posts: 32
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
 
Greenhorn
Posts: 18
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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends Now it is really clear to me
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic