Constructor in Java

0
152
Constructor-In-Java

 

Constructor in java is a special method that is responsible for initializing the object.

Rules to create Java Constructor :

Basically there are differences between method and constructor.

1. Name of the constructor should be same as the class name.
2. Constructor should not have explicit return type.
3. A constructor will be called implicitly when the new instance of object is created.

Types of Java Constructors :

1.Default constructor
2.Parameterized constructor

1.Java Default constructor : A constructor with no parameters or arguments are known as Default constructor in Java.

syntax of Default constructor :

Example of Default Constructor :

OUTPUT : 

Zero Constructor Example In Java

2. Java Parameterized constructor : A Constructor which contains parameters is known as Parameterized constructor.

syntax of Parameterized constructor :

Example of parameteried Constructor :

OUTPUT :

Parameterized Constructor Example In Java

Points to Remember :

1. If we donot create an constructor, Java compiler takes the responsibility of creating an default constructor.

LEAVE A REPLY

Please enter your comment!
Please enter your name here