0% found this document useful (0 votes)
10 views1 page

Java/oop/constructors/constructor - HTML Constructors

Constructors initialize the instance variables of an object when it is created with the new keyword. Constructors must have the same name as the class and can call other constructors in the same class or parent class. If no constructors are defined, a default parameterless constructor is created that initializes variables and calls the parent constructor. Constructors differ from methods by not specifying a return type or containing a return statement.

Uploaded by

Sabari Nathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Java/oop/constructors/constructor - HTML Constructors

Constructors initialize the instance variables of an object when it is created with the new keyword. Constructors must have the same name as the class and can call other constructors in the same class or parent class. If no constructors are defined, a default parameterless constructor is created that initializes variables and calls the parent constructor. Constructors differ from methods by not specifying a return type or containing a return statement.

Uploaded by

Sabari Nathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

http://www.leepoint.

net/notes-
java/oop/constructors/constructor.html
Constructors
When you create a new instance (a new object) of a class using the new keyword, a constructor
for that class is called. Constructors are used to initialize the instance variables (fields) of an
object. Constructors are similar to methods, but with some important differences.
Constructor name is class name. A constructors must have the same name as the class
its in.
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.
Differences between methods and constructors.
o There is no return type given in a constructor signature (header). The value is this
object itself so there is no need to indicate a return value.
o There is no return statement in the body of the constructor.
o The first line of a constructor must either be a call on another constructor in the
same class (using this), or a call on the superclass constructor (using super). If
the first line is neither of these, the compiler automatically inserts a call to the
parameterless super class constructor.
These differences in syntax between a constructor and method are sometimes hard to see
when looking at the source. It would have been better to have had a keyword to clearly
mark constructors as some languages do.
this(...) - Calls another constructor in same class. Often a constructor with few
parameters will call a constructor with more parameters, giving default values for the
missing parameters. Use this to call other constructors in the same class.
super(...). Use super to call a constructor in a parent class. Calling the constructor for
the superclass must be the first statement in the body of a constructor. If you are satisfied
with the default constructor in the superclass, there is no need to make a call to it because
it will be supplied automatically.

You might also like