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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

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 …