Ticker

6/recent/ticker-posts

Constructor in Java

Constructor in Java

What is a Constructor in Java?
A constructor in Java is a special method that is automatically called when an object of a class is created. It is used to initialize the instance variables of the class and perform any necessary setup required for the object's state. Constructors have the same name as the class and do not have a return type. They can be used to ensure that the object is in a valid state when it is created.

Types of Constructors:

  1. Default Constructor: It is automatically provided by Java if no constructor is explicitly defined in the class. It takes no arguments and initializes instance variables to their default values (e.g., 0 for numeric types, null for object references).

  2. Parameterized Constructor: This type of constructor allows you to pass arguments while creating an object, enabling you to initialize instance variables with specific values.

Syntax of Constructor:

java
public class MyClass {
// Default Constructor
public MyClass() {
// Initialization or setup code here
}

// Parameterized Constructor
public MyClass(int arg1, String arg2) {
// Initialization or setup code using arg1 and arg2 here
}
}

Example of Constructors in Java:

  1. Default Constructor Example:
java
public class Car {
String make;
String model;
int year;

// Default Constructor
public Car() {
make = "Unknown";
model = "Unknown";
year = 0;
}

public void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}

public static void main(String[] args) {
Car myCar = new Car(); // Default constructor called automatically
myCar.displayInfo();
}
}

Output:


Make: Unknown
Model: Unknown
Year: 0
  1. Parameterized Constructor Example:
java
public class Student {
String name;
int age;

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

public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public static void main(String[] args) {
Student student1 = new Student("Alice", 20);
student1.displayInfo();

Student student2 = new Student("Bob", 22);
student2.displayInfo();
}
}

Output:


Name: Alice
Age: 20
Name: Bob
Age: 22

Explanation:

  • In the first example, a Car class is defined with a default constructor that initializes the instance variables make, model, and year to default values. When Car myCar = new Car(); is called, the default constructor sets the values, and then the displayInfo() method is called to print the car information.

  • In the second example, a Student class is defined with a parameterized constructor that takes name and age as arguments and assigns them to the instance variables. When Student student1 = new Student("Alice", 20); and Student student2 = new Student("Bob", 22); are called, the parameterized constructor sets the provided values for each student, and then the displayInfo() method is called to print their information.

Constructors are essential for initializing objects and ensuring that the objects are properly set up before they are used in the program.

Post a Comment

0 Comments