Chris Dormani Chris Dormani - 1 year ago 85
Java Question

What would be the default constructor header

I am new to java and trying to grasp concepts concerning headers for the default constructor.

The header for the first constructor in Circle is:

public Circle(String label, int radius)

If one decides to add a default constructor to the class. What would be the header for this default constructor?

I have looked online but not really seen a succinct answer.

Answer Source

If one decides to add a default constructor to the class.

You don't decide to add a default constructor. The compiler adds one for you if you don't specify any constructors at all.

The one it adds is defined by JLS§8.8.9:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

  • The default constructor has the same accessibility as the class (§6.6).

  • The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).

  • The default constructor has no throws clauses.

  • 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 if Circle is public, then the default's signature would be:

public Circle()

e.g., the full generated version is:

public Circle() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download