Java Constructors
A Java constructor is a special block of code that runs when an object is created. Constructors are used to give a new object its starting field values.
Instead of creating an object and then assigning every field separately, a constructor can set up the object in one clear step.
Constructor Syntax
A constructor has the same name as the class and no return type. Not even void is written before a constructor name.
class Student {
String name;
int grade;
Student() {
name = "Unknown";
grade = 1;
}
void printInfo() {
System.out.println(name + " is in grade " + grade + ".");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.printInfo();
}
}
Output:
Unknown is in grade 1.
The line new Student() creates a Student object and calls the Student() constructor. The constructor assigns initial values to name and grade.
Constructors With Parameters
Constructors often use parameters so each new object can start with different values. The arguments are passed inside the parentheses after new ClassName.
class Book {
String title;
String author;
int pages;
Book(String bookTitle, String bookAuthor, int pageCount) {
title = bookTitle;
author = bookAuthor;
pages = pageCount;
}
void printSummary() {
System.out.println(title + " by " + author + " has " + pages + " pages.");
}
}
public class Main {
public static void main(String[] args) {
Book first = new Book("Java Basics", "Ava Reed", 240);
Book second = new Book("Objects First", "Noah Kim", 180);
first.printSummary();
second.printSummary();
}
}
Output:
Java Basics by Ava Reed has 240 pages.
Objects First by Noah Kim has 180 pages.
The Book constructor receives three values and stores them in the object’s fields. Each Book object keeps its own title, author, and page count.
Why Use Constructors?
Constructors make object creation safer and clearer. They help make sure an object starts in a useful state before other code tries to use it.
- They keep setup code inside the class.
- They reduce repeated field assignments in
main. - They make required starting values obvious.
For example, a Book needs a title and author to be meaningful. A constructor lets the class ask for those values when the object is created.
Default Constructors
If a class does not define any constructor, Java automatically provides a no-argument constructor. This is called a default constructor.
However, once you write any constructor yourself, Java does not add the default no-argument constructor for you. If you still want new ClassName() to work, you must write that constructor yourself.
Overloaded Constructors
A class can have more than one constructor, as long as the parameter lists are different. This is called constructor overloading.
class Rectangle {
int width;
int height;
Rectangle() {
width = 1;
height = 1;
}
Rectangle(int size) {
width = size;
height = size;
}
Rectangle(int rectWidth, int rectHeight) {
width = rectWidth;
height = rectHeight;
}
int area() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
Rectangle small = new Rectangle();
Rectangle square = new Rectangle(5);
Rectangle poster = new Rectangle(4, 6);
System.out.println(small.area());
System.out.println(square.area());
System.out.println(poster.area());
}
}
Output:
1
25
24
Java chooses which constructor to call by looking at the arguments. new Rectangle() calls the no-argument constructor, new Rectangle(5) calls the one-parameter constructor, and new Rectangle(4, 6) calls the two-parameter constructor.
Common Mistakes
- Writing a return type on a constructor, such as
void Student(). That creates a method, not a constructor. - Forgetting that the constructor name must exactly match the class name.
- Calling
new Book()when the class only has a constructor that requires arguments. - Repeating setup code in many places instead of putting it in a constructor.
Takeaway: constructors run when objects are created, and they are the usual place to give an object its required starting values.
