wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static constant problem 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 "static constant problem" Watch "static constant problem" New topic
Author

static constant problem

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hiii

can anybody pls explain me the following link

http://qa.jtiger.org/GetQAndA.action?qids=13&showAnswers=true

i m not able to understand its explanation...
sometime he is saying that particular thing is constant and
sometime.... a same type of code i.e with same modifier i.e static final
he is saying that its not constant in other class..


help me to understand it...

regards,


Thanks and Regards, Amit Taneja
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
In all cases where the static initializer executes, the field being referenced is not a constant, but a mere final.
JLS 15.28 makes it quite clear what a constant is.
A final and a constant are two different things.
This example proves it.
[ April 11, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hii

in your link eg, according to you the V.S and X.I are constants
and not others why ? they looks like the same and have same modifiers..
in all other classes why all other field are not constants..

just asking ...as me didn't still clear..

if i m not wrong ..constants have to be both final and static and must be intialized before constructor completes..
is i am wrong ?


regards,
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Again, I refer you to JLS 15.28.
There is a link down the bottom of the page.
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi Tony,
I am not understand the difference. let me know your commands (don't refer JLS )


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Dude,
There are tow things here which u have to take care of:
1) Compile time constants
2) Final fields.

Eg:
static final Integer I = new Integer(7); // Not a compile time constant
static final int I = 7; // compile time constant

There is a link in the JLS doc which describes about class initialization, it has four points with respect to that and one of those is:

A static field declared by T(the class) is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.


initialization

So u can see that the class gets initialized only when the reference to the field is not a compile time constant. About compile time constants u can read the link given by toni or also the explanation given in ur Question link. So according to ur code only class U, W, Y and Z satisfies the requirement of initialization.

Thats the reason why u r getting such output.

Hope its clear now
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
still not clear .......sorry ?

in class V there is object initialization and in class X primitive initialization

not clear
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
class x{
final static int x=1;
static {
System.out.println("X");
}
}

class y{
final static Integer X = new Integer(5);
static {
System.out.println ("Y");
}
}

class xy{
final static int xx=10;
final static Integer yy = new Integer(77);
static {
System.out.println("xy");
}
}


class BB {
public static void main(String args[])
{
int a = x.x;
int b=y.X.intValue();
int c=xy.xx;//class xy is not initialized here
int d=xy.yy.intValue(); //class xy is initialized

}
}

1. class x has compile-time constant expression. so x.x is resolve at
compile time.
2. class y don't have compile-time constant. so It will be initialized.
3. class xy has compile time constant and also has not compile-time
constant.but this class is not initialized when we refere the field
xy.xx but class xy is initiailized if we refer the field xy.yy
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi

i understand that the static initialization are taking place when the class is first loaded.
when we accessing the constant variables belongs to a class the class won't get loaded.

if we access non-constant variable or creating instance or static variables the class get loaded.
at this point the initialization take place.

if i am wrong in the above point do comment me..


but i confused at this point

why the object s is not considered as the constant

static final Object S = "s";

when string is considered as the static.
static final String S = "s";
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hii raghu
i understand what u typed
but just confirm me that
is initailization means that we can use that variable by creating instance of that class..?
and if you are saying that "compile time constant" does that means that it will be initialized when the class load i.e when we compile the program even before running it ?

pls anybody confirm me ? so i will get the rt knoledge

thanx
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi amit,
Initialization means when the code

int b=y.X.intValue();

execute, the class loader load the class and execute the static method if the field is static. Other wise we cannot refer field name withot creating object. This process is called Initialization.Then also class will be initialized if we create a object of the class.

Compile-time constant means if the sinal static field is literal rather than class type it is called compile time constant. for example
static final int b=100; This is initialized in compile time. so it is called as compile time constant.
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
Hi,
String literals,primitive values and certain operations involving both are compile time constants.
So, static final Object S="s" is not compile time constant whereas
static final String S="s" is considered as compile time constant.
 
GeeCON Prague 2014
 
subject: static constant problem