Constructors in java

8
0

Introduction

A constructor in Java is a special block of code used to initialize an object. It is called automatically when an object of a class is created using the new keyword. Constructor usually assigns initial values to an object’s attributes.

Features of Java Constructors

  • A constructor has the same name as its class.

  • It does not have a return type, not even void.

  • It is called automatically when an object is created.

  • It can initialize an object’s attributes.

  • A class can contain multiple constructors.

  • Constructors can use access modifiers such as public, private, and protected.

Syntax of a Constructor

class ClassName {
ClassName() {
// Constructor body
}
}

Types of Constructors in Java

The main types of constructors in Java are:

  1. Default constructor

  2. No-argument constructor

  3. Parameterized constructor

1. Default Constructor

When a class does not contain any constructor, the Java compiler automatically provides a no-argument constructor. This compiler-provided constructor is called the default constructor.

It initializes fields with their default values, such as:

  • 0 for integer types

  • 0.0 for decimal types

  • false for boolean

  • null for reference types

Java provides a default constructor only when no constructor has been explicitly declared.

// Declare a class without explicitly defining any constructor
class Student {
// Define a data member with a default value
String name = "Aman";
// Define a method to display the student's name
void display() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
// Create an object using the default constructor provided by Java
Student student = new Student();
// Display the value stored in the object's data member
student.display(); // Aman
}
}

Default Constructor

Default Constructor

2. No-Argument Constructor

A no-argument constructor is a constructor written by the programmer that does not accept any parameters.

// Declare a class with a data member and a no-argument constructor
class Student {
String name;
// Define a no-argument constructor to initialize the student's name
Student() {
name = "Unknown";
}
// Define a method to display the student's name
void display() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
// Create an object, which automatically calls the no-argument constructor
Student student = new Student();
// Display the value initialized by the constructor
student.display(); // Unknown
}
}

Here, Student() is a no-argument constructor. It assigns Unknown as the initial value of name.

No argument Constructor

No argument Constructor

3. Parameterized Constructor

A parameterized constructor accepts values when an object is created. It allows different objects to start with different data.

// Declare a class with data members and a parameterized constructor
class Student {
String name;
int age;
// Define a parameterized constructor to initialize the object's data members
Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}
// Define a method to display the student's details
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
public class Main {
public static void main(String[] args) {
// Create an object and pass arguments to the parameterized constructor
Student student = new Student("Aman", 20);
// Display the values initialized by the constructor
student.display(); // Aman is 20 years old.
}
}

In this example, "Aman" and 20 are passed to the constructor and assigned to the object’s attributes.

Parameterized Constructor

Parameterized Constructor

Constructor Overloading

Constructor overloading means creating multiple constructors in the same class with different parameter lists.

For example, a class may contain:

class Student {
String name;
int age;
// No-argument constructor
Student() {
name = "Unknown";
age = 0;
}
// Constructor with one parameter
Student(String name) {
this.name = name;
age = 0;
}
// Constructor with two parameters
Student(String name, int age) {
this.name = name;
this.age = age;
}
// Display the student's details
void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// Calls the no-argument constructor
Student student1 = new Student();
student1.displayDetails();
// Calls the constructor with one argument
Student student2 = new Student("Aman");
student2.displayDetails();
// Calls the constructor with two arguments
Student student3 = new Student("Rahul", 21);
student3.displayDetails();
}
}

Java selects the appropriate constructor based on the number and types of arguments passed while creating the object.

Constructor Overloading

Constructor Overloading

Summary

A constructor in Java is a special block of code that initializes an object when it is created using the new keyword. It has the same name as the class, no return type, and can initialize an object's attributes.

Java supports default, no-argument, and parameterized constructors, allowing objects to be initialized in different ways. Constructor overloading allows multiple constructors with different parameter lists, and Java selects the appropriate one based on the arguments provided.

CS Core

Read Similar Blogs

Comments0