• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static constant problem

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, I refer you to JLS 15.28.
There is a link down the bottom of the page.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,
I am not understand the difference. let me know your commands (don't refer JLS )
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still not clear .......sorry ?

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

not clear
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic