Java Constructor – A Complete Guide

What is a Constructor in Java?

A constructor is a special method used to initialize an object. It has the same name as the class and is automatically called when an object is created.

2. Key Features of a Constructor:

  • It has the same name as the class.
  • It does not have a return type (not even void).
  • It is automatically called when an object is created.

3. Types of Constructors in Java

A. Default Constructor (No Arguments Constructor)

A constructor that does not take any parameters. It is used to assign default values to instance variables.

Example

javaCopyEditclass Post {
    String title;
    String content;

    // Default Constructor
    Post() {
        title = "Default Title";
        content = "This is a default post content.";
    }

    void display() {
        System.out.println("Title: " + title);
        System.out.println("Content: " + content);
    }
}

public class Main {
    public static void main(String[] args) {
        Post myPost = new Post(); // Calls default constructor
        myPost.display();
    }
}

Output:

vbnetCopyEditTitle: Default Title
Content: This is a default post content.

B. Parameterized Constructor

A constructor that takes arguments to initialize an object with specific values.

Example: Creating a WordPress Post

javaCopyEditclass Post {
    String title;
    String content;

    // Parameterized Constructor
    Post(String postTitle, String postContent) {
        title = postTitle;
        content = postContent;
    }

    void display() {
        System.out.println("Title: " + title);
        System.out.println("Content: " + content);
    }
}

public class Main {
    public static void main(String[] args) {
        Post myPost = new Post("Java Constructors", "This post explains Java constructors with examples.");
        myPost.display();
    }
}

Output:

vbnetCopyEditTitle: Java Constructors
Content: This post explains Java constructors with examples.

C. Constructor Overloading

A class can have multiple constructors with different parameter lists. This is known as constructor overloading.

Example: Overloaded Constructors for WordPress Post

javaCopyEditclass Post {
    String title;
    String content;

    // Default Constructor
    Post() {
        this("Untitled Post", "No content available.");
    }

    // Parameterized Constructor
    Post(String title, String content) {
        this.title = title;
        this.content = content;
    }

    void display() {
        System.out.println("Title: " + title);
        System.out.println("Content: " + content);
    }
}

public class Main {
    public static void main(String[] args) {
        Post defaultPost = new Post(); // Calls default constructor
        Post customPost = new Post("WordPress Guide", "How to create a post in WordPress.");

        defaultPost.display();
        System.out.println("-----------------");
        customPost.display();
    }
}

Output:

vbnetCopyEditTitle: Untitled Post
Content: No content available.
-----------------
Title: WordPress Guide
Content: How to create a post in WordPress.

D. Copy Constructor (Manually Implemented in Java)

A copy constructor is used to create a new object with the same values as an existing object.

Example: Duplicating a WordPress Post

javaCopyEditclass Post {
    String title;
    String content;

    // Parameterized Constructor
    Post(String title, String content) {
        this.title = title;
        this.content = content;
    }

    // Copy Constructor
    Post(Post existingPost) {
        this.title = existingPost.title;
        this.content = existingPost.content;
    }

    void display() {
        System.out.println("Title: " + title);
        System.out.println("Content: " + content);
    }
}

public class Main {
    public static void main(String[] args) {
        Post originalPost = new Post("Java Basics", "This post covers Java basics.");
        Post copiedPost = new Post(originalPost); // Calls Copy Constructor

        copiedPost.display();
    }
}

Output:

makefileCopyEditTitle: Java Basics
Content: This post covers Java basics.

E. Using “this" Keyword in Constructor

The this keyword is used to:

  1. Refer to the current object.
  2. Call another constructor within the same class.

Example: Using this to Call Another Constructor

javaCopyEditclass Post {
    String title;
    String content;

    // Default Constructor
    Post() {
        this("Default Title", "Default Content");
    }

    // Parameterized Constructor
    Post(String title, String content) {
        this.title = title;
        this.content = content;
    }

    void display() {
        System.out.println("Title: " + title);
        System.out.println("Content: " + content);
    }
}

public class Main {
    public static void main(String[] args) {
        Post myPost = new Post(); // Calls default constructor
        myPost.display();
    }
}

Output:

vbnetCopyEditTitle: Default Title
Content: Default Content

F. Using “super" Keyword in Constructor

The super keyword is used to call the parent class constructor.

Example: Extending WordPress Post with BlogPost

javaCopyEditclass Post {
    String title;
    String content;

    Post(String title, String content) {
        this.title = title;
        this.content = content;
    }
}

class BlogPost extends Post {
    String category;

    BlogPost(String title, String content, String category) {
        super(title, content); // Calls Parent Constructor
        this.category = category;
    }

    void display() {
        System.out.println("Title: " + title);
        System.out.println("Content: " + content);
        System.out.println("Category: " + category);
    }
}

public class Main {
    public static void main(String[] args) {
        BlogPost myBlog = new BlogPost("Java OOP", "This post explains Java OOP concepts.", "Programming");
        myBlog.display();
    }
}

Output:

makefileCopyEditTitle: Java OOP
Content: This post explains Java OOP concepts.
Category: Programming

G. Private Constructor (Singleton Pattern)

A private constructor prevents object creation from outside the class. It is used in the Singleton Design Pattern.

Example: Singleton for a WordPress Post Manager

javaCopyEditclass PostManager {
    private static PostManager instance;

    // Private Constructor
    private PostManager() {}

    // Static method to get the instance
    public static PostManager getInstance() {
        if (instance == null) {
            instance = new PostManager();
        }
        return instance;
    }

    void createPost() {
        System.out.println("New post created.");
    }
}

public class Main {
    public static void main(String[] args) {
        PostManager manager1 = PostManager.getInstance();
        PostManager manager2 = PostManager.getInstance();

        manager1.createPost();
        System.out.println(manager1 == manager2); // Output: true (same instance)
    }
}

Output:

sqlCopyEditNew post created.
true

4. Conclusion

✅ Java constructors initialize objects automatically.
✅ They can be default, parameterized, overloaded, copied, or private.
this and super help in constructor chaining.
Singleton pattern uses a private constructor.

Leave a Reply

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