DEV Community

Praveen
Praveen

Posted on

Constructer in java

A constructor in Java is a special method that is automatically called when you create an object. Its main purpose is to initialize the object's data (fields).
Key Features of a Constructor
It has the same name as the class.
It does not have a return type (not even void).
It is called automatically when an object is created using the new keyword.
Types of Constructors
Default (No-Argument) Constructor – Takes no parameters.
Parameterized Constructor – Takes one or more parameters to initialize the object.
Why Use Constructors?
To initialize object values when they are created.
To avoid writing separate initialization methods.
To ensure objects start in a valid state.

Top comments (0)