Question regarding final blank variable initilisation

W

walidhaouari

Plz can someone explain the difference between static final variable
and final instance variable
regarding their initilisation: in particular : I noticed that if we
leave a final INSTANCE variable non initialised, the code will compile
fine (unless we make a call to it) while it's not the case for a
static final variable.

I tryed to look everywhere but it's always mentioned that if we leave
a final variable not initilized it will result in compile time error!
Can someone explain the rule about that?
Thanks;

Walid Haouari
 
E

Eric Sosman

Plz can someone explain the difference between static final variable
and final instance variable

A static variable -- final or not -- is associated with
the class itself and not with any particular object of the
class. There is "one copy" of a static variable, no matter
how many of the class' objects exist (there is one copy even
if zero objects exist).

A non-static or "instance" variable -- final or not --
exists as part of an object created from the class. Every
object has its own version of the instance variable; there
are exactly as many "copies" of the instance variable as
there are objects of the class (if there are zero objects,
there are zero instance variables).

A final variable -- static or instance -- cannot have a
new value assigned to it after initialization. Since a static
variable is created when the class is loaded, a static final
must be initialized at that time and never changed afterwards.
The initialization can be written in the declaration

static final int THE_ANSWER = 42;

or can be performed by a static initialization block:

static final int THE_ANSWER;
static {
THE_ANSWER = 42;
}

An instance variable is created each time an object of the
class is created. A final instance variable must be initialized
at that time and never changed afterwards. The initialization
can be written in the declaration

final int theAnswer = 42;

or in a (non-static) initialization block

final int theAnswer;
{
theAnswer = 42;
}

or in each constructor:

final int theAnswer;

MyClass() {
theAnswer = 42;
}

MyClass(double trouble) {
theAnswer = (int)Math.round(trouble);
}
regarding their initilisation: in particular : I noticed that if we
leave a final INSTANCE variable non initialised, the code will compile
fine (unless we make a call to it) while it's not the case for a
static final variable.

That shouldn't be the case. Could you show a short, complete
code sample that demonstrates exactly what you see? I suspect
the variable *is* being initialized, perhaps non-obviously.
 
Joined
Feb 9, 2011
Messages
1
Reaction score
0
Hi Everybody Pls read this Code care fully

class Hello{
public static final int x=20;
static{
System.out.println("Static Block");
}
}

class Test{

public static void main(String[] args)
{
System.out.println(Hello.x);
}
}


If I am executing this code output coming is:
20

but As we are accessing the static member of any class, class Hello should be loaded according to the concept of static members.
So the expected Output is :

static block
20

Now I checked in command prompt by giving the command;

java -verbose Test

It is Showing that only Test class is loaded but Hello is not loaded.

Now the Question is if Hello is not loaded then How that static final x is getting memory???????

I am trying to solve this problem from last 1 month .... please if any one knows about this please tell me the reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top