Menu

Java - Constructor & Constructor Overloading
📰

Java - Constructor & Constructor Overloading

DEV Community: java·Gowtham·15 days ago
#v8k7Z06B
#devtojava#devto#dev#student#constructor
Reading 0:00
15s threshold

1.Constructor in Java:

A constructor is a special member of a class that is used to initialize objects.

A constructor is automatically called when an object is created using the new keyword.

Important Features of Constructors

The constructor name must be the same as the class name.

A constructor does not have a return type, not even void.

It is called automatically when an object is created.

Constructors can have parameters.

Constructors can be overloaded.

Example:

`class Student {

Student() { System.out.println("Constructor Called!"); }

public static void main(String[] args) { Student s1 = new Student(); }

Enter fullscreen mode

Exit fullscreen mode

}`

Output:

Constructor Called!

Explanation:

Student is the class name.

Student() is the constructor.

new Student() creates an object.

The constructor is automatically executed when the object is created.

2.Types of Constructors in Java

Java constructors are commonly explained using two categories:

1.Default Constructor

If we do not write…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.