Constructor in JAVA

Constructor in JAVA

First let’s create simple class for student in Java. Student class will have following attributes
1. ID
2. Name
3. Standard
4. Division
5. Address
6. Date of birth
public class Student
{
    String id, name, std, div, adrs, dob;
}
Now latest try to create an object for student class in our main class Once object has been created now let’s assign values for or above attributes. For assigning a values we need to use instance of class object.
class Student
{
    String id, name, std, div, adrs, dob;
    public void disp()
    {
        System.out.println("n ID : " + id + "n Name : " + name + "n Standard : " + std + "n Division : " + div + "n Address : " + adrs + "n Date of Birth : " + dob + "n");
    }
}

public class ConstructorDemo
{
     public static void main(String []args)
     {
        Student s1 = new Student();
        s1.id = "01";
        s1.name = "Vijay";
        s1.std = "7th";
        s1.div = "A";
        s1.adrs = "Atpatnagar";
        s1.dob = "12th Sep 2000";
        s1.disp();
     }
}
Output :
 ID : 01
 Name : Vijay
 Standard : 7th
 Division : A
 Address : Atpatnagar
 Date of Birth : 12th Sep 2000

But we can see this will consume time and lines as well this will increase LOC. There are only 6 attributes in this class but in real life we will be dealing with classes which has many attributes and it is difficult to use above method to assign values to each and every attribute.
This is where constructor comes in the Play.
There are two type of constructor
1. Parameterized
2. Non-parameterised
when we create an object by default Java calls default constructor we didn’t code this constructor in our class but it is called by default.
Non-Parameterized don’t accept values at the time of object creation, but they can be used to display message.
Parameterized constructors are the one which helps up to accept values at the time of object creation.

Use of constructor.

Most important use of constructor is to assign values to attributes at the time when we create an object.
Following is an example of constructor. We will create a constructor for our student class which will help to assign values to student class attributes at the time of object creation.
There are few rules for creating a constructor
1. There is no return type for constructor
2. Constructor name is same as class’s name
class Student
{
    String id, name, std, div, adrs, dob;
    public Student(String id, String name, String std, String div, String adrs, String dob)
    {
        this.id = id;
        this.name = name;
        this.std = std;
        this.div = div;
        this.adrs = adrs;
        this.dob = dob;
    }
    public void disp()
    {
        System.out.println("n ID : " + id + "n Name : " + name + "n Standard : " + std + "n Division : " + div + "n Address : " + adrs + "n Date of Birth : " + dob + "n");
    }
}

public class ConstructorDemo
{
     public static void main(String []args)
     {
        Student s1 = new Student("01","Vijay","7th","A","Atpatnagar","12th Sep 2000");
        s1.disp();
     }
}
Output :
 ID : 01
 Name : Vijay
 Standard : 7th
 Division : A
 Address : Atpatnagar
 Date of Birth : 12th Sep 2000

In above example “this” keyword indicates that the variable it has been attached to who is global variable and not what the local variable or parameterized variable.
What constructor does is it text values as an argument of object and assign those values two variable which will help us to avoid for the fume unnecessary steps like assigning values to each and every attribute with the help of instance.
Now as per our student class is concerned we have two options.
1. We can use parameterized constructor
Or
2. We can use non parameterized constructor
Both will work the only difference in both situations is how will we assign values to those attributes.
If we use parameterized constructor then constructor will take care of assigning values to attributes.
And if we use non parameterized constructor then and we have to use object’s instance to assign values to those attributes.
This scenario also says that we can overload the constructor because although we only have two constructors default constructor and the one which we created but we can also create more constructors and at the time of object creation we can choose which constructor to be called and this will only happen because java allows constructor overloading.
In above example we need to use “this” keyword because parameters name of our constructor is same as our global variable. During execution of constructor compiler will consider both variables as local variables but if we use “this” keyword with left hand side variables then compiler will consider those variables as global variables.
It is not necessary to assign same name to parameterized variable in constructor but it is more like convention, because if someone ask for description of class then he/she will be difficult for him to understand the exact position of parameters.

Leave a Comment