2

I know that when using:

A a = new A();

It assigns a space in the heap,and then calls the constructor,at last returns the reference. But I am confused about that, if I call a constructor in another one, or when the child constructor calls it's parent's. Will this two ways create series of instances? Or one "new" for "one instance"?

Telerik
  • 167
  • 9

3 Answers3

3

When you write this or super on the first line of a constructor, this calls another constructor to do some of the initialisation work. It DOESN'T create a separate instance.

So, if class A extends class B, and the constructor of A calls super(), this means that the space in memory that has been set aside for the instance of B will have an extra load of initialisation done; but this is done in the SAME space as the rest of the constructor for A. So if you write A myObject = new A();, the steps would be -

  1. Assign space on the heap
  2. Run the constructor for class B (the superclass)
  3. Run the REST of the constructor for class A
  4. Assign the reference to the created space to myObject.
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thank your for you response,as what you said,it seems that each time we "new an instance" for a class that we define,each Object instance was created,right? – Telerik Jan 13 '14 at 02:06
  • If you are asking whether there is a one-to-one correspondence between a call to new, and a case of an object being created, the answer is yes. (Except of course for String literals, autoboxing, and various serialisation tricks). – Dawood ibn Kareem Jan 13 '14 at 02:31
  • Autoboxing does not seem relevant to "new" keyword,does it?Do you mean that "new String()" does not call the constructor of object? – Telerik Jan 13 '14 at 02:50
  • No, I mean that there are a couple of ways an object can get created without there being a call to `new` in the code. `String hello = "Hello";` is one of them. Autoboxing is another. But in general, one call to `new` = one object created. – Dawood ibn Kareem Jan 13 '14 at 03:39
  • ah...I knew something about the mechanism of creating string literal between the "string constant pool" and the "heap",thanks for your answer.David. – Telerik Jan 13 '14 at 03:56
1

Let's follow this example from Java docs:

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

Each of these statements has three parts (discussed in detail below):

  • Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
  • Instantiation: The new keyword is a Java operator that creates the object.
  • Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Therefore, new is a keyword used to create an object and a constructor initializes a new object.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

Generally speaking, there are four way to "initialize" a class:

  1. block {} (rarely used)

  2. membre field initialization private in i=0

  3. static block static {} (class init)

  4. constructor public MyClass(int i)

or 4.1 default constructor

(the constructor is called before the other ones)

but under no circumstances the constructor implies a new instance. New instance is done by new operator. When you call a constructir from another constrcutor (or calling a super class constructor) you are simply "calling a method".

Accept my lack of formalism, it is only a short incomplete summary.

venergiac
  • 7,469
  • 2
  • 48
  • 70
  • Where did you get the idea that 1. (called an _instance initializer block_) is never used? Also, only number 3. is used to initialize a class; all the others are used to initialize instances of a class. Finally, the comment _"the constructor is called before the other ones"_ makes no sense; certainly the static block is executed before any constructor or any instance initializer. Field initializers and instance initializer blocks are executed _during_ the execution of the constructor. See [the JLS, §12.5](http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5). – Ted Hopp Jan 12 '14 at 21:07
  • Ted, I can not give a lecture about the java constructors, I have set out a summary of the possible initializations. Regarding the "not sense", see point 5 of the link that you sent me. Then, after 12 years of java, You're the first guy I know who uses the "initializer block" ... it is not a criticism of course ... – venergiac Jan 12 '14 at 21:30
  • I use the initialiser block. Very rarely. But I can't say never. Do I get to be second? – Dawood ibn Kareem Jan 13 '14 at 02:28
  • Thanks your you two,and I remember that the static block is use the class member,not the instance.right? – Telerik Jan 13 '14 at 02:32
  • Yes, edited according to comments. – venergiac Jan 13 '14 at 06:29