0% found this document useful (0 votes)
12 views

Constructors

Uploaded by

sharmilapv84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Constructors

Uploaded by

sharmilapv84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Constructors

 In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the class is created.
 Every time an object is created using the new keyword, constructor is called.
 It calls a default constructor if there is no constructor available in the class. In such case, Java
compiler provides a default constructor by default.

Types of constructors

 default constructor: A constructor is called "Default Constructor" when it does not have any
parameter.
 Parameterized constructor: A constructor which has a specific number of parameters is called a
parameterized constructor.

Rules for Creating Java Constructor

There are following rules for defining a constructor:

 Constructor name must be the same as its class name.


 A Constructor must have no explicit return type.
 A Java constructor cannot be abstract, static, final, and synchronized.

Constructor Overloading

Constructor overloading in Java is a technique of having more than one constructor with different
parameter lists. They are arranged in a way that each constructor performs a different task. They are
differentiated by the compiler by the number of parameters in the list and their types.

Constructor chaining

Constructor chaining in Java is a practice where one constructor calls another constructor of the same
class or a superclass during object creation. It is typically done using either this() to call another
constructor in the same class or super() to call a constructor in the superclass. By centralizing common
construction logic, constructor chaining helps reduce code redundancy and increases the code's
readability.

Constructor Vs Methods

Constructor Methods
A constructor is a special method that is called A method is a function or a block of code within a
when an object of a class is created. class that defines a behavior or functionality of the
class.
A constructor has the same name as the class and Methods can have different names and return
does not have a return type types.
Constructor will be automatically invoked when Methods are called explicitly after an object is
an object is created. created or even without creating an object if they
are static.
The constructor is called once when an object is Methods can be called multiple times during the
created using the new keyword lifetime of an object. They are invoked using the
object reference or the class name (if static).

Can constructor perform other tasks instead of initialization?

Yes, like object creation, starting a thread, calling a method, etc. We can perform any operation in the
constructor as we perform in the method.

You might also like