|
|
Overloading, Overriding, Runtime Types and Object Orientation - Initialization
Steps that occur when a new instance is created (JLS§12.5)
- memory is allocated for all the instance variables in the class and instance variables in all of it's superclasses
- the instance variables are set to their default values
- the constructor used in the creation expression is called according to the following:
- arguments for the constructor are assigned to newly created parameter variables
- if the constructor begins with this(); invoke the constructor recursively following the same five steps
- if the constructor does not begin with this(), then invoke, explicitly or implicitly, the corresponding superclass constructor using super(). These are processed recursively following the same 5 steps.
- execute the instance initializers and instance variables for this class
- execute the remainder of the constructor body
Example:
class Point {
int x, y;
Point() { x = 1; y = 1; }
}
class ColoredPoint extends Point {
int color = 0xFF00FF;
}
class Test {
public static void main(String[] args) {
ColoredPoint cp = new ColoredPoint();
System.out.println(cp.color);
}
}
When the new instance of ColoredPoint is created:
1. first memory is allocated for the fields 'color' in
ColoredPoint and then for the fields 'x, y' in Point
2. the fields are initialized to their default values
3. the no-arg ColoredPoint constructor is invoked.
As none exists, the superclasses no-arg constructor
is invoked. This is done implicitly ie the compiler
added the default no-arg ctor at compile time
4. the Point ctor does not begin with this() so an
invocation is made to the no-arg ctor for Object
(Point's superclass)
5. any instance variable initializers of Object are
invoked and the body of the no-arg ctor is executed
6. next, all the instance initializers for Point's instance
variables are invoked and the body of the Point
constructor is executed.
7. initializer for instance variables of ColoredPoint
are invoked and the body of the ctor is executed.
JLS §12.4.1
- before a class is initialized it's direct superclass must be initialized but interfaces implemented by the class need not be initialized
- a reference to a class field only causes the initialization of it's class even if it is referred to by a subclass ie if 'taxi' is a static field in 'Super' class and is referenced by 'Sub.taxi'; only 'Super' is initialized; not 'Sub'
- the initialization of an Interface does not implicitly cause initialization of it's SuperInterfaces
JLS §8.8.5.1
- a constructor beginning with this() or super() can not use any class or superclass instance variables as an argument to a parameter
No argument constructor
- ONLY the no-arg constructor is called implicitly when new instances are created
New ClassB instance // extends ClassA, has no ctor
ClassA() ctor
New ClassD instance // extends ClassA, has a no-arg ctor
ClassA() ctor
ClassD() ctor
New ClassF instance with no-args // ClassF extends ClassE
// which extends ClassA
ClassA() ctor
ClassE() ctor
ClassF() ctor
// invoked with different ctor
New ClassF instance with parameter
ClassA() ctor // no-arg ctor's of superclasses implicitly
ClassE() ctor // called
ClassF(String name) ctor
- if the constructor being invoked explicitly calls a superclass constructor then the superclass no-arg constructor is not implicitly invoked
ClassC extends ClassB which extends ClassA
// (no call to super(str) in ClassC(String str))
New ClassC instance created
ClassA() ctor // implicitly called
ClassB() ctor // implicitly called
Hello
// (ClassC(String str) explicitly calls super(str))
New ClassC instance created
ClassA() ctor // implicitly called
In ClassB // explicit call;
// NO implicit call to ClassB()
Hello
| !!! Remember !!! |
- If NO constructor exists, the compiler will add a default no-arg constructor
- The no-arg constructor of all superclasses in the hierarchy will be invoked and executed BEFORE the type constructor is executed UNLESS the type constructor explicitly calls another superclass constructor
- There are NO IMPLICIT invocations to any other constructors
|
Also see
Sun Tech Tip: Constructor and Initialization Ordering
Example Code
|