In this article Constructor in Java we give the information about Constructor in Java is a special type of method which is used to initialize the object. The name of the constructor is always the same as the name of the class, and it has no return type.

Constructor in Java:

Constructor in Java is a special type of method which is used to initialize the object. The name of the constructor is always the same as the name of the class, and it has no return type. Whenever an object of a class is created, the constructor is automatically called.

Types of Constructor:

There are mainly two types of constructors in Java:

  1. Default Constructor:

This is the constructor which is not explicitly defined. If you do not define a constructor, the Java compiler automatically provides a default constructor. It initializes the data members of the object with default values ​​(such as 0 for integers, false for booleans, etc.).

Example:

// First Example

class Example {

int x;

String name;

// No constructor explicitly defined here

}

public class Main {

public static void main(String[] args) {

Example obj = new Example();  // Compiler-provided default constructor call

System.out.println(“x: ” + obj.x);  // Output: x: 0 (default value for int)

System.out.println(“name: ” + obj.name);  // Output: name: null (default value for String)

}

}

//Output:

x: 0

name: null

// Second Example

class Example {

// Default Constructor

Example() {

System.out.println(“This is a default constructor.”);

}

}

public class Main {

public static void main(String[] args) {

Example obj = new Example();  // यह डिफ़ॉल्ट कंस्ट्रक्टर कॉल करेगा

}

}

Output:

This is a default constructor.

  1. Parameterized Constructor:

This is the constructor which takes parameters. It is used to provide values ​​while initializing the object.

Example:

class Example {

int x;

// Parameterized Constructor

Example(int val) {

x = val;

}

void display() {

System.out.println(“Value of x: ” + x);

}

}

public class Main {

public static void main(String[] args) {

Example obj = new Example(10);  // parameterized constructor को call करेगा

obj.display();

}

}

Output:

Value of x: 10

Features of Constructor:

  1. The name of the constructor is the same as the name of the class.
  2. Constructor has no return type.
  3. Constructor is not invoked manually; It is automatically called at the time of creation of the object.
  4. You can overload more than one constructor.

Uses of Constructor:

Object Initialization:

The main function of the constructor is to initialize the properties of the object. It automatically sets the values ​​at the time of object creation.

Code Reusability:

With the help of Constructor Overloading, you can create multiple constructors and increase code reusability. Different constructors allow you to initialize objects of the same class in different ways.

Encapsulation:

Constructor follows the principles of encapsulation because it hides the logic of object initialization inside the class.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *