What is Constructor.?

Constructor is like a method, but it is not exactly a method. It is special type of method which is called by JVM (Java Virtual Machine) automatically at the time object creation. In java class we have default constructor which is created by compiler at the time of compilation.

Why we use Constructor.?

Constructor is used to initialize the instance variables in a java class.

Rules to write the Constructor:

Example of (this) keyword:

( this ) keyword is use to differentiate the same instance variable name and constructor parameter name. In below example constructor parameter name and instance variable name are same. To solve the this problem we use ( this ) keyword. You can use ( this ) keyword with any of the variables.

	
    class A
    {
    	int age;
    	public A(int age)
        {
        	//use of this keyword.
        	this.age = age;
        }
    }
    

What is default constructor.?

In java every class have one default constructor which created by the compiler at the time of compilation. By default compiler add one default constructor look like a method. Which is called by JVM (Java Virtual Machine) at the time of compilation.

Example of default constructor:

	
    class A
    {
    	public void book()
        {
        
        }
    }
    
    class Test1
    {
    	public static void main(String arg[])
    	{
    		System.out.println("Default constructor program..");
        	A obj = new A();
        	obj.book();
    	}
    }
    

In above example we didn't create any constructor. But when you will run it with javap tool. It will show you default constructor which is automatically create by JVM at the time of compilation.

Output after compiling the Test1.class file:

	
    C:\Users\Nature\Desktop\Java>javap Test1
    Compiled from "Test1.java"
    class Test1 {
      Test1();
      public static void main(java.lang.String[]);
    } 
    

Now see the above output of Test1.class file. A Test1() default constructor automatically created by compiler at the time of compilation.

Zero Parameterized Constructor

A construtor have no parameter is called Zero Parameterized Constructor.

Example Zero parameterized Constructor

	
    class A
    {
    	int num;
        //zero parameterized constructor.
        public A()
        {
            num = 10;
            System.out.println("Num = "+num);
        }
    }
    
    class Test2
    {
    	public static void main(String arg[])
        {
        	A obj = new A();
        }
    }
    

Output:

	
    Num = 10
    

Parameterized Constructer

A construtor have parameter is called Parameterized Constructor.

Example of Parameterized Costructor

	
    class A
    {
    	int num;
        //parameterized constructor.
        public A(int i)
        {
            num = i;
            System.out.println("Num = "+num);
        }
    }
    
    class Test2
    {
    	public static void main(String arg[])
        {
        	A obj = new A(20);
        }
    }
    

Output:

	
    Num = 20
    

What is constructor Overloading.?

Constructor overloading is same like a method overloading. A constructor with same name but with different parameters is called Constructor overloading.

Example of Constructor Overloading:

	
    class A
    {
    	int num;
	String name;
        public A(int i)
        {
        	num = i;
		System.out.println("Num = "+num);
        }
	public A(String b)	//constructor overloading.
        {
        	name = b;
		System.out.println("Name = "+name);
        }
    }
    
    class Test2
    {
    	public static void main(String arg[])
        {
        	A obj = new A(10);
		A obj1 = new A("Campuslife");
        }
    }
    

Output:

	
    Num = 10
    Name = Campuslife
    
Advertisement here

Share this Page.