Constructors in Java

Constructors in Java

In Java, a constructor is a special type of method used to initialize an object. Its primary purpose is to set the initial state of an object. Constructors resemble instance methods, but there are several key differences:

  • A constructor's name must be the same as the class name.
  • It cannot have a return type, not even void.
  • It gets called when an object of the class is created.

Let's dive into the details.

1. Types of Constructors

  1. Default Constructor: If you don't define any constructor in your class, the compiler creates one for you, called the default constructor. This constructor does not take any arguments.

  2. Parameterized Constructor: A constructor that has a specific number of parameters is called a parameterized constructor.

2. Basic Usage of Constructors

Default Constructor Example:

public class Student { int id; String name; public static void main(String[] args) { Student s1 = new Student(); // Calls default constructor System.out.println(s1.id); // Outputs: 0 System.out.println(s1.name); // Outputs: null } } 

Parameterized Constructor Example:

public class Student { int id; String name; Student(int i, String n) { id = i; name = n; } public static void main(String[] args) { Student s1 = new Student(1, "John"); // Calls parameterized constructor System.out.println(s1.id); // Outputs: 1 System.out.println(s1.name); // Outputs: John } } 

3. Constructor Overloading

Constructor overloading is when a class contains more than one constructor, each having a different number or type of parameters.

public class Box { double width, height, depth; // Default constructor Box() { width = height = depth = 0; } // Constructor for cube Box(double side) { width = height = depth = side; } // Constructor for box Box(double w, double h, double d) { width = w; height = h; depth = d; } } 

4. The this Keyword with Constructors

In case of parameter shadowing (when class attributes and constructor parameters have the same name), you can use the this keyword to differentiate between the instance variables and parameters.

public class Rectangle { int length, breadth; Rectangle(int length, int breadth) { this.length = length; // Refers to instance variable this.breadth = breadth; // Refers to instance variable } } 

5. Constructor Chaining

In Java, a constructor can call another constructor in the same class using this(). This is useful for constructor overloading.

public class Point { int x, y; // Default constructor Point() { this(0, 0); } // Constructor with one parameter Point(int x) { this(x, 0); } // Constructor with two parameters Point(int x, int y) { this.x = x; this.y = y; } } 

6. The super() Call

In inheritance scenarios, the super() keyword is used in a subclass constructor to call the superclass's constructor.

class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { super(); // Calls parent's constructor System.out.println("Child Constructor"); } } 

Conclusion

Constructors play a crucial role in Java by allowing you to control the initialization of objects. They provide flexibility in object instantiation, ensure that objects are in a valid state when created, and enhance the readability and organization of the code. By understanding constructors and their advanced features like overloading and chaining, you can make your Java classes more robust and maintainable.


More Tags

android-workmanager windows-console fragment-tab-host android-studio-2.2 shadow launch4j equation singleton dotnet-httpclient

More Programming Guides

Other Guides

More Programming Examples