Constructors in Java
A Simple Thought
When you order a customized T-shirt online, you choose your size, color, and design. The company then makes that shirt exactly as you specified.
In Java, you can do something similar with your objects using a constructor.
A constructor is a special method that runs automatically when you create an object.
It helps you set up the initial values of an object without writing extra lines of code afterward.
In this lesson, you will learn how to use constructors to create objects that are ready to use from the moment they are made.
The Project: Student Registration System
You will create a small program that registers a new student by using a constructor to assign their details.
Instead of setting values one by one, the program will pass the name and grade directly when creating the object.
Your output should look like this:
1 2 3 4 5======================== STUDENT REGISTRATION Name: Sara Ahmed Grade: 9 ========================
The details will be assigned automatically when the object is created.
The Concept: Constructors
A constructor is a special method inside a class that has the same name as the class.
It is called automatically when you use the new keyword to make an object.
Here’s what a simple constructor looks like:
1 2 3 4 5 6 7 8 9class Student { String name; int grade; Student(String n, int g) { name = n; grade = g; } }
This constructor takes two parameters, assigns them to the variables inside the class, and sets up the object.
Now you can create an object like this:
1Student s1 = new Student("Sara Ahmed", 9);
This single line creates the object and fills in the data.
Why This Matters
Constructors make your programs cleaner and safer.
They allow you to:
- Create objects with correct values immediately
- Avoid forgetting to assign important information
- Write fewer lines of setup code
Most real-world applications use constructors to prepare their objects automatically, such as registering users, booking tickets, or adding items to a cart.
Let’s Build the Student Registration Program
Here’s the complete program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26class Student { String name; int grade; Student(String n, int g) { name = n; grade = g; } void displayInfo() { System.out.println(" Name: " + name); System.out.println(" Grade: " + grade); } } public class StudentRegistration { public static void main(String[] args) { System.out.println("========================"); System.out.println(" STUDENT REGISTRATION"); Student s1 = new Student("Sara Ahmed", 9); s1.displayInfo(); System.out.println("========================"); } }
When you run this program, the constructor automatically sets the values for the student’s name and grade as soon as the object is created.
There is no need to assign them separately.
How It Works
- The Student class has two variables and a constructor.
- When you create the object with new Student("Sara Ahmed", 9), the constructor runs automatically.
- The parameters "Sara Ahmed" and 9 are passed into the constructor and stored in the variables name and grade.
- The displayInfo() method prints the student’s details.
The constructor acts like a quick setup function for every object you create.
Learn Together with AI
If you are using an AI Copilot, you can explore constructors by asking it to build examples for you.
Try this prompt:
“Write a Java program that uses a constructor to set a student’s name and grade.”
Then ask for modifications like:
- “Add a second student using the same constructor.”
- “Add a default constructor that sets name to ‘Unknown’ and grade to 0.”
- “Add a method that updates the grade later.”
These variations will help you understand how constructors can handle different setups for objects.
Practice Challenge
Create a program that defines a class called Car with two variables, brand and model.
Use a constructor to assign these values when the object is created, and display them using a method.
Your output should look like this:
1 2 3 4 5======================== CAR DETAILS Brand: Toyota Model: Corolla ========================
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26class Car { String brand; String model; Car(String b, String m) { brand = b; model = m; } void displayInfo() { System.out.println(" Brand: " + brand); System.out.println(" Model: " + model); } } public class CarDetails { public static void main(String[] args) { System.out.println("========================"); System.out.println(" CAR DETAILS"); Car c1 = new Car("Toyota", "Corolla"); c1.displayInfo(); System.out.println("========================"); } }
When you run this program, the constructor sets up the car’s brand and model immediately.
You’ve now learned how to make your objects prepare themselves automatically when they are created.
Coming Up Next
You now know how constructors make your code more efficient by setting up objects with the right data from the start.
Next, you will learn about inheritance, one of the most important principles in object-oriented programming. It allows one class to reuse the features of another class.