wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable " Watch "final variable " New topic
Author

final variable

Calanthe Wei
Ranch Hand

Joined: Oct 27, 2001
Posts: 42
see the code below:

the result is:
final = final
Initialization of Loaded
non-final = non-final
And when I add change a little bit in the last sentence, like
public static final String y = "non-final";
the result is:
final = final
non-final = non-final
I am a little confused about these two results, can someone
explain it to me? thanks
regds,
Calanthe

[This message has been edited by Marilyn deQueiroz (edited November 10, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I don't understand where the problem is - since your program does not attempt to change the value of a final variable, the modifier has no effect.
Bill
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by William Brogden:
I don't understand where the problem is - since your program does not attempt to change the value of a final variable, the modifier has no effect.
Bill

William, it is the output. On the second output the
"Initialization of Loaded" is gone.
the result is:
final = final
Initialization of Loaded
non-final = non-final
And when I add change a little bit in the last sentence, like
public static final String y = "non-final";
the result is:
final = final
non-final = non-final
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
One of reasons that triggers a class initialization is
when a static field declared by it is used and the reference
is not a compile time constant. Reference to a comiple-time
constant is resolved at compile time. Its use never cause initialization. (see JLS 12.4.1)

The declaration
public static final String x = "final";
makes x a compile-time constant. (see JLS 15.28)
That's why does not cause initialization of class Loaded.
It's the next statement that causes the initialization.
Similarly, when you change to static final, it becomes a compile-time
constant. So, class Loaded is not initialized.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
it was a useful tip.
thanks
maulin
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
agree with Maulin, Vasavada..

hehehe..
Calanthe Wei
Ranch Hand

Joined: Oct 27, 2001
Posts: 42
Thank you guys.
I got it.
Calanthe
 
wood burning stoves
 
subject: final variable