☰ Topics

Constructor

A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Constructors have no return type, not even void. This is because the implicit return type of class' constructor is class type itself.

Example to illustrate constructor:-

class area
{
	double len,wid;
	area()
	{
		len=4;
		wid=5;
		double res=len*wid;
		System.out.println("Area="+res);  
	}
	public static void main(String[] arg)
	{
		area ob=new area();    //area() gets called
	}
}

                            

OUTPUT:

E:\java\new_java>javac area.java

E:\java\new_java>java area
Area=20.0
                        

Constructors can be paramaterized or non-paramaterized. The above example was an example of an non-paramaterized constructor. In paramaterized constructor, parameters are passed.

Example:-
class area1
{
	area1(double len,double wid)
	{
		double res=len*wid;
		System.out.println("Area="+res);  
	}
	public static void main(String[] arg)
	{
		area1 ob=new area1(4,5);    //area1(double len,double wid) gets called
	}
}

                            

OUTPUT:

E:\java\new_java>javac area1.java

E:\java\new_java>java area1
Area=20.0
                    

Using object as parameters:
class area2
{
	int len,wid;
	area2(int l,int w)
	{
		len=l;
		wid=w;
		System.out.println(len+" "+wid);  
	}
	area2(area2 ob)
	{
		len=ob.len;
		wid=ob.wid;
		System.out.println(len+" "+wid);
	}
	public static void main(String[] arg)
	{
		area2 a1=new area2(4,5);    //area2(int l,int w) gets called
		area2 a2=new area2(a1);     //area2(area2 ob) gets called
	}
}

                            

OUTPUT:

E:\java\new_java>javac area2.java

E:\java\new_java>java area2
4 5
4 5
                    

Types of argument passing:-

Arguments are passed in two ways i.e. call by value and call by reference. Call by value copies the value of the argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument. In call by reference, a reference to an argument is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument specified in the call. This means that the changes made to the parameter will affect the argument used to call the subroutine.

Example of call by value:-
class value
{
	void meth(int i,int j)
	{
		i*=2;
		j*=2;
		System.out.println("Changed value of i and j:"+i+" "+j);
	}
	public static void main(String[] arg)
	{
		value ob=new value();
		int i=5,j=10;
		System.out.println("i and j before call:"+i+" "+j);
		ob.meth(i,j);
		System.out.println("i and j after call:"+i+" "+j);
	}
}

                            

OUTPUT:

E:\java\new_java>javac value.java

E:\java\new_java>java value
i and j before call:5 10
Changed value of i and j:10 20
i and j after call:5 10
                        

Example of call by reference:-
class reference
{
	int i,j;
	reference(int a,int b)
	{
		i=a;
		j=b;
	}
	void meth(reference ob)
	{
		ob.i*=2;
		ob.j*=2;
		System.out.println("Changed value of i and j:"+ob.i+" "+ob.j);
	}
	public static void main(String[] arg)
	{
		reference ob=new reference(5,10);
		System.out.println("i and j before call:"+ob.i+" "+ob.j);
		ob.meth(ob);
		System.out.println("i and j after call:"+ob.i+" "+ob.j);
	}
}

                            

OUTPUT:

E:\java\new_java>javac reference.java

E:\java\new_java>java reference
i and j before call:5 10
Changed value of i and j:10 20
i and j after call:10 20
                        

this keyword:

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Consider the following program:-
class prog
{
	int a=7,b=9;
	prog(int a,int b)
	{
		a=a;   //value of parameter is assigned to 'a' of the contructor itself, no changes is made on global variable
		b=b;		
	}
	public void display()
	{
		System.out.println(a+" "+b);
	}
	public static void main(String[] arg)
	{
		prog ob=new prog(6,4);
		ob.display();
	}
}
                            

OUTPUT:

E:\java\new_java>javac prog.java

E:\java\new_java>java prog
7 9
                        

In the above example, parameters(formal arguments) and instance variables are same. So, we have to use this keyword to distinguish local variable and instance variable.

class exthis
{
	int a=7,b=9;
	exthis(int a,int b)
	{
		this.a=a;     //value of parameter 'a' is assigned to global variable 'a'
		this.b=b;		
	}
	public void display()
	{
		System.out.println(a+" "+b);
	}
	public static void main(String[] arg)
	{
		exthis ob=new exthis(6,4);
		ob.display();
	}
}
                            

OUTPUT:

E:\java\new_java>javac exthis.java

E:\java\new_java>java exthis
6 4
                        

Note:- this keyword is also used to invoke current class method or constructor.