This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi all, I would like to know the exact order of construction of an object. I believe that it should be 1. Static Initializer expressions 2. Static Initializer blocks( order in which they are defined) 3. Constructor header ( super or this � implicit or explicit ) 4. Instance variables initialization / Instance initializer block(s)execution 5. rest of the code in the constructor I need explaination for the following behaviour. Example 1. class test1 { static int i = init(); static int init(){ return j;} static int j = 1; public static void main(String args[]) { System.out.println(i); } } Here the output is 0. This is fine as per Maha anna's discussion. Because the variable is accessed before it is initialized. Consider the same code with slight modification below. Example 2 class test1 { static int i = init(); static int init(){ return j;} static final int j = 1; public static void main(String args[]) { System.out.println(i); } } Now the output is 1. How this is happening.
Regards, pasupathi J
nitin sharma
Ranch Hand
Joined: Feb 24, 2001
Posts: 290
posted
0
I think because static final member varaible's are not initialized by the compiler.U get the final value which in any case would be the assigned value,final varaible's are constant,therefore value's to final varaible's can only be assigned just one not twice,Now think if compiler initialise the final variable with the value 0 then it would not have benn able to assign the value 1 to the final varaible,because value's to the final are provided just once not again and again.
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Pasupathi J, Would you please read the JavaRanch Name Policy and re-register using a name that complies with the rules. Thanks for your cooperation. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform