Reputation: 25
I understand that there are three parts to object creation:
classA{}
classB extends classA{}
classA obj = new classB(1,1);
It has to create an object using new operator
and this object must have all the fields related to classB
(initialized to default values does this step give a call to default constructor?) this is instantiation
does it mean this step is initialization using java's default constructor?
this object is then passed down the hierarchy giving calls to various constructor in the way to get initialized (which comes under initialization) and the final obj is created by 'classB(1,1)` constructor whith required Initializations
but how is the object mentioned in instantiation step being created initially with all available fields?
please point out if anything i said is wrong
Upvotes: 0
Views: 411
Reputation: 234857
Roughly, the way it works is:
Object
itself) must, as their first step, call another constructor in the same class or call a constructor in the superclass. (If you don't code one of these calls in a constructor, the compiler automatically inserts a call to super();
—the default superclass constructor—at the start of that constructor. Each constructor in the chain—starting with Object
at the deepest level of constructor calls—does whatever setting of instance fields in the object.The second step might be modified slightly if there are instance initializers in the class declaration. See Chapter 8 of the Java Language Specification for details.
Upvotes: 1
Reputation: 109613
If the class has no constructor a default constructor is implicitly defined.
Constructors have method name <init>
in a stacktrace.
A constructor call does the following:
A a = ...;
) are initialized by doing the assignments.Upvotes: 2