How does the default Java constructor work?

Asked

Viewed 1,127 times

8

What is the default Java constructor like? This is it?

public Pessoa(){
    super();
}
  • The pattern comes implicitly (even without creating), but just like you did is explicit. The super(); is not mandatory.

  • but if I don’t put anything in it already puts it in for me right? so I can inherit the constructor from the java.lang Object class, correct?

  • No, they can be invoked as you did, but they’re not implicitly so. Just to reinforce http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

  • @Leonardo was thinking one thing, deep down you have two questions in one, could you separate them? Leave the constructor’s here and pass the signature to a new question. We try to keep the questions well organized to help other people in the future and ideally do not mix two subjects that although they are close, is not about the same thing.

  • I have separated the question from the signature since it is quite different from the part about constructor. A new question can be found here.

  • @Leonardo Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

Show 1 more comment

1 answer

10

The standard builder (default constructor) is the one the compiler creates for you. If you create it, it’s not the default. You can even simulate one equal to default:

public Pessoa() { super(); }

This is the way that a default constructor is mounted for you when you do not create it. The default constructor never has parameters and has nothing in the body other than a reference to the upper class. It exists only to start the creation of the object, it does nothing.

In fact the call of super() in this case it is unnecessary. See also: What is the function of the super in a Java constructor?.

Is in the language specification.

More about builders.

  • Beauty, but the Default constructor contains the super() implicitly?

  • @Bigown, can you explain his second question (@Leonardo)? 'Cause if I’ve got the sequence right, it’s name + number of parameters + type, and it’s confusing for me. If so, send an example.

  • @Leonardo I am finishing the answer. Cold, I will improve.

  • Is that really a line neh @bigown? : ). Although I think I should focus much more on the question of constructors.

  • @Cold just suggested to AP.

  • Okay. How about the question of saying "name + number of parameters +type of the parameters"? I didn’t quite get the idea on that. What would represent the amount of parameters?

  • 1

    A short complement: if you declare a constructor with arguments in the class, Java does not add the default constructor. If you want that in addition to the argument constructor there is one without arguments, you have to add the latter explicitly.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.