0

I understand that there are three parts to object creation:

  1. Declaration
  2. Instantiation
  3. Initialization

classA{}
classB extends classA{}
classA obj = new classB(1,1);

Instantiation

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?

Initialization

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

4
  • from my bare understanding: 1. ... give a call to default constructor? No. There is not necessary any default constructor for a class. It simply means that the memory is allocated and all members are initialized with default values. 2. ... final object is returned to...constructor which retruns final object Constructor is not returning anything Commented Jul 2, 2015 at 7:20
  • How is this memory allocated(object created) that too of a type classB in first place during instantiation Commented Jul 2, 2015 at 7:27
  • the "class" itself should have such information: It know what fields (hence, how much memory, and how fields aligned) is required for itself, and also knows the memory its parent needs. Conceptually it is how it works. If you need to real implementation detail on how it works then it is out of my knowledge :) Commented Jul 2, 2015 at 7:30
  • And when I said "the constructor returns" I meant to say that the final object is created using the values given in classB(1,1) constructor sorry if it caused some sort of confusion Commented Jul 2, 2015 at 7:31

2 Answers 2

2

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:

  1. the object is created, with all fields nulled: 0, 0.0, null, ...
  2. the super constructor is called, possibly implicit if not present in the code.
  3. all initialized fields (A a = ...;) are initialized by doing the assignments.
  4. the rest of the constructor is executed.
Sign up to request clarification or add additional context in comments.

Comments

1

Roughly, the way it works is:

  1. The new bytecode instruction is executed with a symbolic reference to the class of the object to be created. As described in the Java Virtual Machine Specification, this allocates a new object in the garbage collected heap and initializes all fields to their default values.
  2. One of the object's constructors is called. (Which one is called depends on what was coded.) All constructors (except for 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.