codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

28 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java Constructors
☕Java Programming

Java Constructors

Updated 2026-04-20
3 min read

Java Constructors

In Java, constructors are special methods that are used to initialize objects of a class. They have the same name as the class and do not return any value, not even void. This tutorial will cover the fundamentals of constructors, including their syntax, types, and best practices.

Introduction to Constructors

Constructors play a crucial role in object-oriented programming by setting up the initial state of an object when it is created. They are called automatically when a new instance of a class is created using the new keyword.

Basic Syntax

A constructor has the following syntax:

public ClassName() {
    // Initialization code here
}
  • Access Modifier: The access modifier (e.g., public, private) determines where the constructor can be accessed.
  • Constructor Name: It must match the class name exactly.
  • No Return Type: Constructors do not have a return type, not even void.

Example

public class Car {
    private String model;
    private int year;

    // Constructor
    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    // Method to display car details
    public void displayInfo() {
        System.out.println("Model: " + model + ", Year: " + year);
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Toyota Camry", 2021);
        myCar.displayInfo();
    }
}

In this example, the Car class has a constructor that takes two parameters: model and year. When a new Car object is created, the constructor initializes these fields.

Types of Constructors

Java supports several types of constructors:

1. Default Constructor

If no constructor is defined in a class, Java provides a default constructor with no arguments. This constructor does nothing but initialize the object's fields to their default values (null for objects, 0 for integers, etc.).

public class NoConstructor {
    private int value;

    // No explicit constructor

    public void setValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

2. Parameterized Constructor

A parameterized constructor is a constructor that takes one or more parameters to initialize the object's fields.

public class Person {
    private String name;
    private int age;

    // Parameterized constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display person details
    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

3. Copy Constructor

A copy constructor is used to create a new object as a copy of an existing object.

public class Point {
    private int x;
    private int y;

    // Parameterized constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // Copy constructor
    public Point(Point other) {
        this.x = other.x;
        this.y = other.y;
    }

    // Method to display point coordinates
    public void display() {
        System.out.println("Point: (" + x + ", " + y + ")");
    }
}

4. No-Argument Constructor

A no-argument constructor is a constructor that does not take any parameters.

public class EmptyClass {
    private String message;

    // No-argument constructor
    public EmptyClass() {
        this.message = "Default Message";
    }

    // Method to display message
    public void showMessage() {
        System.out.println(message);
    }
}

Constructor Overloading

Constructor overloading allows a class to have multiple constructors with different parameter lists. This provides flexibility in how objects can be created.

public class Book {
    private String title;
    private String author;

    // No-argument constructor
    public Book() {
        this.title = "Unknown";
        this.author = "Unknown";
    }

    // Parameterized constructor
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    // Copy constructor
    public Book(Book other) {
        this.title = other.title;
        this.author = other.author;
    }

    // Method to display book details
    public void displayInfo() {
        System.out.println("Title: " + title + ", Author: " + author);
    }
}

In this example, the Book class has three constructors: a no-argument constructor, a parameterized constructor, and a copy constructor.

Best Practices

  1. Initialize Fields: Always initialize fields in the constructor to ensure that objects are always in a valid state.
  2. Use Access Modifiers: Use appropriate access modifiers for constructors to control their visibility.
  3. Avoid Overloading with Too Many Constructors: Keep the number of constructors reasonable to avoid confusion and maintainability issues.
  4. Document Constructors: Use Javadoc comments to document constructors, explaining their purpose and parameters.

Conclusion

Constructors are essential components of Java classes, responsible for initializing objects. Understanding different types of constructors and best practices will help you write robust and maintainable Java code. By following the guidelines presented in this tutorial, you can effectively use constructors to manage object creation and initialization in your Java applications.


PreviousJava Class MethodsNext Java Modifiers

Recommended Gear

Java Class MethodsJava Modifiers