Create a class with one integer instance variable. Initialize the variable using :(i) Default constructor.(ii) Parameterized constructor.

A constructor in object-oriented programming is a special method that is responsible for initializing the attributes or properties of an object when it is created. Constructors play a crucial role in the process of creating and initializing objects in a class. Two common types of constructors are the default constructor and the parameterized constructor.

Introduction to Java Classes and Constructors

In Java, a class is a blueprint for creating objects. Objects are instances of a class, and each object has its own set of attributes (instance variables) and behaviors (methods). Constructors are special methods that are called when an object is created. They initialize the object’s state and set values to its instance variables.

Creating a Class with an Integer Instance Variable

Let’s create a simple class named NumberHolder with one integer instance variable named value.

public class NumberHolder {
    // Integer instance variable
    private int value;

    // Default Constructor
    public NumberHolder() {
        // Initialize the instance variable in the default constructor
        this.value = 0;
    }

    // Parameterized Constructor
    public NumberHolder(int value) {
        // Initialize the instance variable in the parameterized constructor
        this.value = value;
    }

    // Getter method to retrieve the value
    public int getValue() {
        return value;
    }

    // Setter method to update the value
    public void setValue(int value) {
        this.value = value;
    }
}

Default Constructor:

A default constructor is a constructor with no parameters. It is automatically provided by Java if you don’t define any constructors in your class. In the NumberHolder class, we explicitly define a default constructor to set the initial value of the value variable to 0.

// Default Constructor
public NumberHolder() {
    // Initialize the instance variable in the default constructor
    this.value = 0;
}

Here, this.value = 0; initializes the value instance variable to 0 when an object of the NumberHolder class is created using the default constructor.

Parameterized Constructor:

A parameterized constructor is a constructor with parameters that allow you to initialize the instance variables with specific values when an object is created. In the NumberHolder class, we define a parameterized constructor to set the initial value of the value variable based on the parameter passed during object creation.

javaCopy code

// Parameterized Constructor public NumberHolder(int value) { // Initialize the instance variable in the parameterized constructor this.value = value; }

Here, public NumberHolder(int value) is the parameterized constructor, and this.value = value; initializes the value instance variable with the value passed as a parameter.

Using the Class:

Now, let’s use the NumberHolder class in a simple Java program to demonstrate the use of both constructors.

public class Main {
    public static void main(String[] args) {
        // Using the Default Constructor
        NumberHolder defaultHolder = new NumberHolder();
        System.out.println("Default Constructor - Initial Value: " + defaultHolder.getValue());

        // Using the Parameterized Constructor
        NumberHolder parameterizedHolder = new NumberHolder(42);
        System.out.println("Parameterized Constructor - Initial Value: " + parameterizedHolder.getValue());

        // Updating the value using the setter method
        parameterizedHolder.setValue(100);
        System.out.println("Updated Value: " + parameterizedHolder.getValue());
    }
}

In this program:

  • We create an object defaultHolder using the default constructor, and its initial value is printed.
  • We create an object parameterizedHolder using the parameterized constructor with an initial value of 42, and its initial value is printed.
  • We update the value of parameterizedHolder using the setter method and print the updated value.

Conclusion:

In this explanation, we’ve created a simple Java class NumberHolder with one integer instance variable and demonstrated the use of both a default constructor and a parameterized constructor. Understanding these concepts is fundamental to object-oriented programming in Java, as constructors play a crucial role in initializing objects and their state.

Add a Comment

Your email address will not be published. Required fields are marked *