Java – Constructor with Example3 min read

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 (that is object) is created and is used for the initialization of the data members 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.

Types of Constructors in java:

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

Default constructor(no-argument constructor):

This constructor does not have any parameter and is 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:

Car is created

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 the null character.


Basic Example for displaying the default values in Java:

Output:

0 null 0
0 null 0


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:

Output:

101 John 30
105 Ron 32


Constructor Overloading in Java:

Constructor Overloading is like a method of 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 their data types differs for each constructor by the compiler.

Example of Constructor Overloading in Java :

Output:

Displaying three parameterized constructor
101 Prashant 30

Displaying three parameterized constructor
105 Karan


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More