Originally posted by Hanna Habashy:
If I have a class that has the folowing members:
1- static variabls.
2- static block.
3- public variabls.
4- static methods
5- private variabls.
6- public method
and a public constructor.
when I instantiate the class from somewhere invoking "new"
what is the order of intializing the previous members??
thank you
When you compile a
java program, the compiler generates an two initilization methods, namely <clinit> and <init>. The <clinit> method is the class initialization method that is invoked whenever a class is first loaded. The <init> method is the instance initialization method that is invoked when an instance is created.
The <clinit> method consists of static variable initializers and the codes in the static initializers. The order will depend in the order they are declared in the source codes.
The <init> method consists of (in the following order):
1. call to the superclass <init> method.
2. instance variable initializers(regardless of their access modifiers) and instance initializers; again the order depends on the order they are declared in the source
3. the rest of the codes of the constructor.
Hope this helps