which class has a default constructor? (Req. guidance on constructor)
Given:
class X {}
class Y {Y() {}}
class Z {z(int i) {}}
which class has a default constructor?
Is it true? That class X and Z have default constructor? or X, Y and Z all three have default constructor?
As constructor have same name in same case as class name, with no return type and may or may not have one or many parameters.
Re: which class has a default constructor? (Req. guidance on constructor)
Classes are automatically given default constructors if no constructor is defined. So, class X will be given a default constructor, while class Z will not. Obviously, a parameterless constructor is provided in Y.
Quote:
•Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).
•Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.
Java: Constructors