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)?
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 ?
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.
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!