jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When a final is not a constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When a final is not a constant " Watch "When a final is not a constant " New topic
Author

When a final is not a constant

JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
I found following qestion on qa.jtiger.org



What is the output and why?

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
Can you pls share that explanation?


SCJP 1.5<br />SCWCD 1.4
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
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
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
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
If you compile and run this code, you do get answer D.

Guy
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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?


Tony Morris
Java Q&A (FAQ, Trivia)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[DKT]: Must be a wrong question.

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)."

Follwing the link to 4.12.4 we find:

"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
Now it makes sense... thanks Jim Yingst
JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
thx for explanation
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: When a final is not a constant
 
Similar Threads
June Newsletter Puzzle
output clarification
HELP~~
Super and sub class
how the execution happens?