File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Initializer Expression 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 Initializer Expression" Watch "Static Initializer Expression" New topic
Author

Static Initializer Expression

Pasupathi J
Greenhorn

Joined: Feb 21, 2001
Posts: 4
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
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
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Initializer Expression