• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final & static member, wich first initialized?

 
horri khaled
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have one question about the order of initialization members.
given :


wich first initialized i or j? and why?

thank in advance

Khaled
 
Viorel Craescu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All static members get initialized first because static members are initialized when the class is first time loaded. Remember static variables are class members NOT instance members.
All instance members are initialized when the object is constructed, using new operator. Even i, is a final variable, it's still an instance member so get initialized when the constructor is ran and can't be changed after.

If you decompile your code you will see something like this:


Probably you were thinking to static final variables, which are also called compiled time constants which is totally something else. A final variable IT IS NOT a compile constant. To be a compile time constant static keyword need to be added(static final) and the variable need to be a primitive or to have a wrapper type.


Viorel
[ April 25, 2008: Message edited by: Viorel Craescu ]
 
horri khaled
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Viorel,

All instance members are initialized when the object is constructed, using new operator. Even i, is a final variable, it's still an instance member so get initialized when the constructor is ran and can't be changed after

what you say right now is right, but not always, take this code :

and the output is :

before Object creation:
i :12
j :2
a :0
After object creation:
i :12
j :2
a :12

if you remove the final key word, you have sth like this
before Object creation:
i :0
j :2
a :0
After object creation:
i :12
j :2
a :12

that means that final members are initialized before the constructor is run.
(correct me if i am wrong ), but before or after the static? this what i search.

Khaled
 
Viorel Craescu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables gets initialized BEFORE instance variables.
What you did there doesnt make sense.
That init method is ran inside the constructor, so you can't say it's before the constructor. You actually do samething in both places.
Final instance variables get initialized before non-final instance variables, but not before static variables.
These are the steps when you call the constructor for your object InitialTest:

1. All static variables get initialized with their given values or default values.
2. All instance variables get initialized with their default values. Final instance variables get initialized with the given value.
3. super() constructor is ran.
4. All instance variables get init with their given values.


That's how the compiler transform your class:


The order will be like this:
1. static final variables
2. static variables
3. final variables
4. non-final non-static variables


Viorel
[ April 25, 2008: Message edited by: Viorel Craescu ]
 
horri khaled
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Viorel for this explication.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic