Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter4 Review Question #12 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.Why the answer of this question is 8..I dont understand this? please help me




 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nil. Hatamova,

First of all, a warm welcome to CodeRanch!

Nil. Hatamova wrote:Hi.Why the answer of this question is 8..I dont understand this? please help me

What do you think the output would have been for this code snippet? This might give a better idea where your thoughts are incorrect.

Kind regards,
Roel
 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought the answer will be 02
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:i thought the answer will be 02

I see. This gives a very good insight in your thought process, so I'm able to give (hopefully) a crystal-clear answer

You think this code will print 0But this code is known as an instance initializer block. And such a block is only executed if an instance is created (using the new operator), hence its name. This instance initializer block is defined in the RopeSwing class. So when a RopeSwing instance is created, this instance initializer block will be executed. But in this example no instance are created and therefor this code is never executed.

And you'll probably think that because of this linethis line in the main methodwill print 2. But if you look closely at the Rope class, you'll notice that the length variable is marked static, so length is a class (static) variable. So it's not an instance variable. Unlike an instance variable (which is associated with an instance/object), a class (static) variable is associated with the class. Every instance of the class shares a class (static) variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. And that's exactly what's happening here: the class (static) variable length is first set to 2 (on line rope1.length = 2;) and then it's set to 8 (on line rope2.length = 8;). Remember: it seems that the length variable is an instance variable, but it's definitely not one as it's marked static. It's a class (static) variable, so only one variable exists which is shared by every instance of the Rope class. And although it's considered a bad practice, you can use any object of that class to access a class (static) variable. So the exam might try to confuse/mislead you using this codeBut this code is completely equivalent with the less confusing/misleading codeUsing this code it's really obvious that length is a class (static) variable. Expect on the exam to encounter both possibilities to access a class (static) member.

Now let's see if you understand all the above. It's time for a pop quiz question. The original code snippet needs only 2 (small) changes to produce the output you were expecting (printing 0 and 2). Can you tell which ones (or just share the adjusted code snippet)?

Hope it helps!
Kind regards,
Roel
 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Roel for clear answer. it is so helpful for me
I add here RopeSwing rs = new RopeSwing(); for print 0


And i remove static keyword from static int length.

Is something wrong here?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:Thank Roel for clear answer. it is so helpful for me

Glad to hear you find the explanation helpful

Nil. Hatamova wrote:Is something wrong here?

You are absolutely spot-on! These 2 (small) changes will produce the output you initially expected. Have a cow!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic