1

There is something that I don't understand about the real role of the default constructor in java. In the official tutorial about object creation :

Creating Objects

All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.

And in the docs about the Default Constructor (§8.8.9)

8.8.9. Default Constructor

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

So even the default constructor of the class Object has an empty body. And I know that the default constructor does NOT initialize fields to their default values, because it's the compiler who does that :

Default Values

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler.

What I don't understand is, if we didn't declare a constructor, what does the default constructor really do ?

Community
  • 1
  • 1
Younes HAJJI
  • 375
  • 3
  • 21
  • 4
    It _exists_. It allows you to write `new Foo()` without writing `public Foo() { }`. – Marko Topolnik Nov 10 '16 at 09:47
  • support you to create the Object – Ye Win Nov 10 '16 at 09:47
  • @MarkoTopolnik thank you for your simple and logical answer, how can I mark my question answered by you ? – Younes HAJJI Nov 10 '16 at 10:55
  • 2
    Note that there is a funny effect of the generated default constructor behavior; if you add a non-default constructor to a class with no constructors, your code will not compile anymore if any code was calling the (generated) default constructor. And more deviously, if there are any subclasses which are implicily calling `super();. – Adriaan Koster Nov 10 '16 at 11:02

4 Answers4

3

what does the default constructor really do?

It calls super(). As per all your quotations. And does nothing else. JLS #8.8.9:

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

i.e. it does nothing else. For those who believe it initializes instance variables please see JLS #12.5 where the contrary is asserted.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • it calls super() if it has a super class, if it is not the case it call the Object default constructor, which has an empty body. So what is the purpose for that ? – Younes HAJJI Nov 10 '16 at 09:53
  • And allows the class to be instantiated. Though that's more what it is and not what it does. – biziclop Nov 10 '16 at 09:53
  • 1
    @YounesHAJJI Without calling a constructor of the super class, you couldn't instantiate the class at all. – biziclop Nov 10 '16 at 09:53
  • @YounesHAJJI If it doesn't have a superclass the superclass is `Object`, in which case it still calls `super()`. Have a look at the generated code. This is a distinction without a difference. The purpose in both cases is for the superclass to initialize itself. The superclass may or may not need to do anything to accomplish that, but that's not a concern of the derived class. – user207421 Nov 10 '16 at 09:57
  • I know that if a class doesn't have a superclass the superclass is `Object`, and call it's default constructor which has an empty body, so in this case it does NOT initialize itself as you said ! I think till now the most logical answer is :It allows you to write new Foo() without writing public Foo() { } as @MarkoTopolnik did mention in his comment. – Younes HAJJI Nov 10 '16 at 10:20
  • 1
    @YounesHAJJI You are arguing with both the JLS and the object code. If you think you have a more logical answer I can only suggest you post it. You don't appear to have understood the point about initialising the superclass at all. It is the function of the derived class to ensure that the base class gets an opportunity to initialize itself, and the compiler ensures it. What the base class does with that opportunity, if anything, is not the derived class's business. – user207421 Nov 10 '16 at 10:24
  • This is what the default constructor does. If you don't believe it, you can always check the compiled class file to see the generated code the default constructor contains. It is simply **not** a matter of opinion or argument. – biziclop Nov 10 '16 at 10:27
  • @EJP you did said 'The purpose in both cases is for the superclass to initialize itself', and I said that if the superclass is `Object` it's default constructor which has an empty body, so in this case it does NOT initialize itself. – Younes HAJJI Nov 10 '16 at 10:27
  • @YounesHAJJI Of course it does, that's silly. That the initialisation is a no-op is neither here nor there. For all the subclass knows, `Object`s no-arg constructor may have code in it. Or it may not. It doesn't matter. – biziclop Nov 10 '16 at 10:28
  • @YounesHAJJI The fact the `Object` constructor doesn't do anything doesn't change the *purpose* of calling `super()`. You aren't making sense. – user207421 Nov 10 '16 at 10:33
  • Three downvotes for a correct answer with two relevant citations is really a bit much, guys. – user207421 Nov 10 '16 at 10:36
  • @EJP @biziclop I didn't said that the default constructor doesn't call `super()`. I know that it does, If the class is not `Object`. But if we have a class that doesn't extends a superclass, so it's default constructor call the `Object` default constructor (because every class extends `Object`), which has an empty body and doesn't so anything, it does not initialize it self. – Younes HAJJI Nov 10 '16 at 10:44
  • 1
    @YounesHAJJI **Every** class except `Object` extends a superclass. So every class except `Object` calls `super()`, which gives every superclass *an opportunity* to initialize itself, *whether it actually takes that opportunity or not.* It's perfectly simple. Unclear what you're really asking here. – user207421 Nov 10 '16 at 10:47
  • @EJP I'm sorry for being unclear in my question, and thank you for the explication – Younes HAJJI Nov 10 '16 at 11:30
  • 1
    According to the spec you linked, in step 4 the instance initializers are run and in step 5 the rest of the constructor body runs. Therefore instance initializers run as a part of constructor invocation. Not before it and not after it, but in the middle of it (after the initial `super()` call). – Marko Topolnik Nov 10 '16 at 13:48
2

What I don't understand is, if we didn't declare a constructor, what does the default constructor really do ?

By default, if no constructor is declared, a class has a default constructor with no args.
I think that's why, by default all constructor calls super(). It follows probably the convention over configuration principle.

Whatever you declare a public constructor or you don't declare at all constructor, first instruction of the constructor is super().
That's why if you define in a class MyClass a constructor with args MyClass(String s) without keeping a constructor with no argument, constructor of MyClass subclasses cannot compile while it doesn't precise in their first instruction, the call to an existing parent constructor, in the exemple, it would be super(String ...).

Here an example :

public class MyClassWithNoArg{
  public MyClassWithNoArg(){
  }
}

MyClassWithNoArg constructor calls super() in this first instruction even if it not specified in the source code.

It is as if it is written in this way :

public class MyClassWithNoArg{
  public MyClassWithNoArg(){
     super();
  }
}

Imagine now another class MyClassWithArg:

public class MyClassWithArg{
  public MyClassWithNoArg(String s){
  }
}

And MySubclass a subclass of MyClassWithArg

public class MySubclass extends MyClassWithArg{
  public MySubclass (String s){
  }

  public MySubclass (){
  }
}

In both cases, it will not compile since as explained previously all constructors call by default the default constructor (super()) of their parent but here the parent,MyClassWithArg, has no constructor with no arg. So, it doesn't compile.

So to solve the problem, you have to call the super constructor which exists.

public class MySubclass extends MyClassWithArg{
  public MySubclass (String s){
     super(s);
  }

  public MySubclass (){
    super("");
  }
}

Now, compilation is OK.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Whenever you declare a constructor you can provide a `super()` call with whatever arguments you like as the first instruction, and whether the constructor is public or private is irrelevant. – user207421 Nov 10 '16 at 10:00
  • 1
    Have I told the contrary ? Have I refer about private modifier ? Can you read with careful and kindness answers which don't come from you ? – davidxxx Nov 10 '16 at 10:02
  • Yes you certainly have. It isn't my function here to explain your own answer to you. – user207421 Nov 10 '16 at 10:04
  • The fact that the compiler provides a default constructor has nothing do with the other fact that all constructors except those of `Object` call `super()` one way or the other. You are getting further and further off the point, and not answering the actual question. – user207421 Nov 10 '16 at 10:30
  • Maybe but I think it may help to understand the logic behind default constructors in Java. Since by default, classes have an empty arg constructor, it seems meaningful that by default classes call the super constructor with empty arg. The super() auto called follows the principle of Convention over configuration. Convention is a default arg constructor for any class, so it seems helpful to call by default for classes the super constructor with no arg. – davidxxx Nov 10 '16 at 10:43
  • Of course, but that's not what it says in your over-elaborate, fallacious, and largely irrelevant answer. – user207421 Nov 10 '16 at 10:48
  • I don't agree with you. Question is "What does the default constructor really do?" I try to answer as I think it should be. You don't like my answer ? You have the right but please, don't be so disrespectful. It is helpless. – davidxxx Nov 10 '16 at 10:56
2

In Java, if you don´t declare any constructor the compiler create a default constructor and this constructor call to super() method, that is parents constructor. And in this process, inits instance variables like no-default constructors.

Juanjo
  • 21
  • 4
0

Without constructor no one can create a object of a class. Now if programmer does not add any constructor with a class then java compiler added a constructor with that class before compilation,which is known as default constructor in java.

But if programmer adds any kind of constructor(with argument or without argument) then that becomes user defined constructor and no constructor 'll be added via java compiler.

Now question is what does constructor do.

  1. allocates memory for new object
  2. if user defines any other operation like initialising variable that is done.
KOUSIK MANDAL
  • 2,002
  • 1
  • 21
  • 46