Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, a constructor is a special method that is used to initialize objects. The constructor is called when an object of a class is created. Here are some key details about constructors in Java:

1. Definition and Syntax

A constructor has the same name as the class and does not have a return type. It is invoked automatically when an object of the class is instantiated.

				
					class ClassName {
    // Constructor
    ClassName() {
        // initialization code
    }
}

				
			

2. Types of Constructors

Java supports two types of constructors:

Default Constructor (No-Argument Constructor)

If no constructor is defined in a class, Java provides a default constructor with no arguments

				
					class MyClass {
    // Default constructor
    MyClass() {
        System.out.println("Default constructor called");
    }
}

				
			

Parameterized Constructor

A constructor that accepts one or more parameters.

				
					class MyClass {
    int x;

    // Parameterized constructor
    MyClass(int i) {
        x = i;
    }
}

				
			

3. Constructor Overloading

Java allows multiple constructors within a class, each with a different parameter list. This is known as constructor overloading.

				
					class MyClass {
    int x;
    String y;

    // Default constructor
    MyClass() {
        x = 0;
        y = "Default";
    }

    // Parameterized constructor
    MyClass(int i) {
        x = i;
    }

    // Another parameterized constructor
    MyClass(int i, String s) {
        x = i;
        y = s;
    }
}

				
			

4. Calling Constructors

When creating an object, the appropriate constructor is called based on the arguments provided.

				
					public class Test {
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(); // Calls default constructor
        MyClass obj2 = new MyClass(10); // Calls parameterized constructor with one parameter
        MyClass obj3 = new MyClass(20, "Hello"); // Calls parameterized constructor with two parameters
    }
}

				
			

5. this() Keyword

The ‘this()' keyword is used to call another constructor from within a constructor in the same class. It helps in constructor chaining

				
					class MyClass {
    int x;
    String y;

    MyClass() {
        this(0, "Default");
    }

    MyClass(int i) {
        this(i, "Default");
    }

    MyClass(int i, String s) {
        x = i;
        y = s;
    }
}

				
			

6. Superclass Constructors

If a class is inheriting another class, the subclass constructor calls the superclass constructor using the ‘super‘ keyword.

				
					class SuperClass {
    SuperClass() {
        System.out.println("Superclass constructor called");
    }
}

class SubClass extends SuperClass {
    SubClass() {
        super(); // Calls superclass constructor
        System.out.println("Subclass constructor called");
    }
}

				
			

7. Initialization Blocks

In addition to constructors, Java provides initialization blocks (both instance and static) that can be used to initialize fields.

				
					class MyClass {
    int x;
    String y;

    // Instance initialization block
    {
        x = 10;
        y = "Instance Initialization";
    }

    MyClass() {
        System.out.println("Constructor called");
    }
}

class Test {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}

				
			

8. Restrictions on Constructors

  • Constructors cannot be abstract, static, final, or synchronized.
  • They cannot have a return type, not even void.

Summary

Constructors in Java are essential for creating and initializing objects. They provide flexibility in object creation through overloading and chaining, and they ensure that an object’s state is set up correctly upon creation. Understanding how to use constructors effectively is fundamental to mastering Java programming.

Scroll to Top