Constructor in Java
A constructor in Java is a special method within a class blueprint that gets called whenever a new object of that class is created.
Note:
● The constructor’s name must exactly match the class name.
● Constructors do not have a return type (such as void or int).
● The constructor is automatically called when an object is created.
All classes have constructors by default: if you do not explicitly define one, Java provides a default constructor. However, the default constructor doesn’t allow you to initialize fields with specific values.
Constructors help save time and improve readability. Take a look at the example below to understand why.
Basic Structure :
class ClassName {
// Constructor definition
public ClassName() {
// Constructor body
// Initialization code goes here
}
}
Constructors can also accept parameters, allowing fields to be initialized at the time the object is created.
The following example demonstrates a constructor with parameters. We define two parameters, String modelName and String colorName, which are used to initialize the fields Model and Color. When we create objects such as car1 (new Car(“Toyota”, “Blue”)) and car2 (new Car(“BMW”, “Red”)), we set Model and Color for each car.
class Car {
public String model;
public String color;
// Constructor with parameters
public Car(String modelName, String colorName) {
model = modelName;
color = colorName;
}
// Method to simulate car driving
public void drive() {
System.out.println(“The ” + color + ” ” + model + ” car is driving…”);
}
}
public class Program {
public static void main(String[] args) {
Car car1 = new Car(“Toyota”, “Blue”);
Car car2 = new Car(“BMW”, “Red”);
car1.drive();
car2.drive();
}
}
Output:
The Blue Toyota car is driving…
The Red BMW car is driving…
Explanation:
In the Car class, we define two fields of String type to hold the model and color of the car:
public string Model;
public string Color;
Then, we create a constructor to receive values for modelName and colorName from each object and initialize the model and color fields:
public Car(String modelName, String colorName) {
model = modelName;
color = colorName;
}
After defining the constructor, we create a custom method called drive to display the car’s model and color:
public void drive() {
System.out.println(“The ” + color + ” ” + model + ” car is driving…”);
}
Finally, we create a Program class with the main method, which serves as the entry point for the program. Here, we create instances (car1 and car2) of the Car class, pass parameters to the constructor to set model and color, and then call the drive method to display each car’s information.
public class Program {
public static void main(String[] args) {
Car car1 = new Car(“Toyota”, “Blue”);
Car car2 = new Car(“BMW”, “Red”);
car1.drive();
car2.drive();
}
}
Constructors Save Time
Consider the following example to understand how constructors can reduce code duplication:
By using constructors, you can eliminate repetitive code and make your code cleaner and more efficient.
Without Constructor | With Constructor |
---|---|
class Car { public String model; public String color; |
class Car { public String model; public String color; |
No explicit constructor. Fields are manually assigned after object creation. | Constructor is created to initialize model and color fields during object creation. |
N/A |
public Car(String modelName, String colorName) { model = modelName; color = colorName; } |
public void drive() { System.out.println(“The ” + color + ” ” + model + ” car is driving…”); } |
public void drive() { System.out.println(“The ” + color + ” ” + model + ” car is driving…”); } |
public class Main { public static void main(String[] args) { Car car1 = new Car(); car1.model = “Toyota”; car1.color = “Blue”; car1.drive(); Car car2 = new Car(); car2.model = “BMW”; car2.color = “Red”; car2.drive(); } } |
public class Main { public static void main(String[] args) { Car car1 = new Car(“Toyota”, “Blue”); car1.drive(); Car car2 = new Car(“BMW”, “Red”); car2.drive(); } } |
Task:
1. Basic Constructor:
Create a class named Employee with the following properties:
● String Name
● int Id
● double Salary
Implement a constructor that initializes these properties. Create an object of the Employee class in the main method, initialize it using the constructor, and print the properties to the console.
2. Parameterized Constructor:
Create a class named Product with the following properties:
● String ProductName
● String Category
● double Price
Implement a parameterized constructor that takes arguments to initialize these properties. Create an object of the Product class in the main method, initialize it using the parameterized constructor, and print the properties to the console.
3. Default and Parameterized Constructor:
Create a class named Student with the following properties:
● String Name
● int RollNumber
● String Course
Implement a default constructor that initializes these properties with default values. Implement a parameterized constructor that takes arguments to initialize these properties. Create objects using both constructors in the main method and print the properties to the console.
Course Video
YouTube Reference :
It’s a free online tutorial on Java constructors for beginners.
You’ll learn about Java constructors, their purpose, types, and how to use them effectively.
No certificates are provided, but you’ll gain valuable programming skills.
Constructors ensure objects are initialized properly, providing a clean and efficient way to set up object state.
Yes, the course covers constructor overloading and how to create multiple constructors with different parameter lists.
You’ll learn how to define constructors, their purpose, types, and how to use them for object initialization.
Yes, the course includes practical examples for both default and parameterized constructors to solidify your understanding.