This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Joined: Mar 01, 2005
How do you explain U.S based on your logic ? Is it not a constant to be inlined at compile time?
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.
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
Joined: Mar 01, 2005
Now it makes sense... thanks Jim Yingst
Joined: May 23, 2005
thx for explanation
Joined: Sep 24, 2003
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com