Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

When a final is not a constant

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you pls share that explanation?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr Nalla

How do you explain U.S based on your logic ? Is it not a constant to be inlined at compile time?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very confusing.

U and V look very similar to me. Doesn't make any sense.
Must be a wrong question.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you compile and run this code, you do get answer D.

Guy
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Rupak Khurana
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it makes sense... thanks Jim Yingst
 
JigaR Parekh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx for explanation
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic