Java – Constructor with Example3 min read

What is Constructor?

A constructor is a block of codes just like a method in Java. It is a special type of method that is called when an instance of the class is created and is used for the initialization of a newly created object.

There are some rules for Java constructor:

  • The constructor name must be the same as its class name.
  • It should not have any return type not even void.

Note: Default Constructor is used if the user does not define a constructor then the java compiler creates a default constructor and initializes the member variables to its default values. That is numeric data types are set to 0, char data types are set to null character.

There are two types Java Constructor:

  • Default constructor(no-argument constructor)
  • Parameterized constructor.

Default constructor(no-argument constructor):

This constructor does not have any parameter and hence called a no-argument constructor.

Syntax of default constructor in Java:

Basic Example: Program to create and call a default constructor in Java:

Output of Default constructor:

Basic Example  for displaying the default values in Java:


Java Parameterized Constructor:

A Parameterized Constructor is a constructor having a specific number of parameters. The parameter is added to a constructor as they are added to the methods that are inside the parentheses of the constructor.

The data-type must match with values that are passed and in that particular order and number.

Example of Parameterized Constructor in Java:


Constructor Overloading in Java:

Constructor Overloading is like a method overloading in Java. This is a technique in Java where a single class has more than one Constructor and each of them has a different parameter.
The number of parameters and its data-types differs each constructor by the compiler.

Example of Constructor Overloading in Java :



Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More