a) TUVWXYZ b) UWXYZ c) TVWXYZ d) TUWYZ e) TXWYZ f) no output g) compile-time error ________ h) runtime exception ________ i) something else ________
Answer is D
They have given some explanation about this but i am not able understand this can any body discuss this more ?? thx.
Rupak Khurana
Ranch Hand
Joined: Mar 01, 2005
Posts: 89
posted
0
Can you pls share that explanation?
SCJP 1.5<br />SCWCD 1.4
Ramakrishna Nalla
Ranch Hand
Joined: Apr 21, 2005
Posts: 61
posted
0
Here is their explanation:
The correct answer is d). The important point here is the concept of a constant expression, which is defined in the Java Language Specification Second Edition 15.28. Note that a final does not necessarily imply a constant expression, or simply, "a final is not necessarily a constant". A static initialiser executes when a class is first loaded, so clearly, the class V and X are never loaded by the class loader. By no strange coincidence, the V.S field and X.I field are constant expressions, while the fields in the other classes are not constants, merely final fields. Since the constant expressions are "inlined" at compile-time, that is, any references are replaced with their constant value, there is no reference to the constant fields and so no reference to their enclosing classes. Therefore, they never load, and their static initialiser never executes. Also note that 'null' is not a constant, and therefore, T.S is not inlined.
Rupak Khurana
Ranch Hand
Joined: Mar 01, 2005
Posts: 89
posted
0
Mr Nalla
How do you explain U.S based on your logic ? Is it not a constant to be inlined at compile time?
Devender Thareja
Ranch Hand
Joined: Jul 14, 2005
Posts: 187
posted
0
Very confusing.
U and V look very similar to me. Doesn't make any sense. Must be a wrong question.
Devender Thareja
SCEA, SCBCD, SCJP
Guy Allard
Ranch Hand
Joined: Nov 24, 2000
Posts: 776
posted
0
If you compile and run this code, you do get answer D.
Guy
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Very confusing.
U and V look very similar to me. Doesn't make any sense. Must be a wrong question.
It may be confusing to some, but it certainly isn't a 'wrong question'. Given that I am the author of that question, by best explanation is provided alongside it, therefore, I dare not attempt to try to explain it again for fear of further confusing. Do you have any specific question perhaps? Have you taken a look at JLS 15.28?
Of course, it's quite easy to compile and run the program to see what happens. That won't tell you exactly why the programbehaves as it does, but it will tell you whether the given answer is correct or not.
[Rupak]:
Mr Nalla
How do you explain U.S based on your logic ?
It's not Mr. Nalla's logic - he's quoting Tony Morris's explanation, as he said.
As for the field U.S - it's not a compile-time constant according to JLS 15.28. The closest it comes is this line:
"Qualified names of the form TypeName . Identifier that refer to constant variables (�4.12.4)."
"We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable."
The variable U.S is not a constant variable because, although it meets all the other requirements, its type is not primitive or String - it's Object. Even though the actual value assigned to the variable is a String, the declared type of the variable is Object. Thus, U.S is not a compile-time constant.
"I'm not back." - Bill Harding, Twister
Rupak Khurana
Ranch Hand
Joined: Mar 01, 2005
Posts: 89
posted
0
Now it makes sense... thanks Jim Yingst
JigaR Parekh
Ranch Hand
Joined: May 23, 2005
Posts: 112
posted
0
thx for explanation
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
I can't help but find the expression 'constant variable' slightly amusing
Constant \Con"stant\, n. That which is not subject to change; that which is invariable. [1913 Webster]
Variable \Va"ri*a*ble\, n. That which is variable; that which varies, or is subject to change. [1913 Webster]
So, a constant variable is something which is invariable, which varies, which is not subject to change, which is subject to change.