Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 32
1
  • Likes 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




 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought the answer will be 02
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • 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
Ranch Hand
Posts: 32
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

for this question I selected "The code does not compile" and I was surprise to see is not the correct one.
I understood the logic of 8. But for me this would be the right answer only if the static member was named "LENGTH" and not "length".
In the previous question (10) the Class Rope is defined with the static member LENGTH. Here is imported static all static members and then is used "length" instead of "LENGTH". Why would this compile since Java is case sensitive ?
 
Marshal
Posts: 68135
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Victor Calin wrote:. . . I understood the logic of 8. But for me this would be the right answer only if the static member was named "LENGTH" and not "length".
In the previous question (10) the Class Rope is defined with the static member LENGTH. . . .

Welcome to the Ranch

In the code given, which I presume matches what is given in the book, the field is spelt “length” because it is not a constant. It is independent of the code in the previous question, unless it says to use code from another question.
 
author & internet detective
Posts: 39890
798
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor: As Campbell noted, questions are independent of each other. On the real exam, they are always independent for a few reasons:
  • they are on different screens so you wouldn't be able to see the previous question
  • the real exam randomly picks questions for a bank so it would be hard to preserve links/order


  • In our books, the questions are independent something like 99.999% of the time. (The only time I can think of that we didn't have independent questions was when we asked a few questions about a diagram. This was to avoid having to keep reprinting the diagram.)

    In our books and practice exam, we often have similar looking questions. We start with the same idea (ex: Rope) and change different things about it. We do this to make it harder to memorize the answers - intentionally or accidentally.) This allows the reader to do the review questions many times to reinforce the material. It's too easy for the brain to say "oh , the Rope question has the answer D" and gloss over the point (ex: constants.) So we have lots of Rope questions! Forces the brain to focus on the Java key points!
     
    Victor Calin
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you both for the clarifications. It all make sense now, thanks
     
    I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!