Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Constructors in Java

In this article we will talk about the constructors in Java and see how to work with it.

The java programming support different types of methods as well as user define method or predefine method. Generally Java supports such types of method that can be invoked implicitly and explicitly in both ways. Invoke means calling the method. If programmer uses implicitly invoking of the method then they have taken a concept of Constructor.

Constructor is a special type of method that name is same as class name. As the constructor name and the class name both are same so we have to take care about our class name. Because in generally java program name and class name both are same and here we find out that class name and method name both are same for the creation of constructor. Opposite of constructor is called destructor. Generally constructor is called at time of staring program where destructor is called at the time of the end of the program.

Generally when we create different types of a class then the data members are automatically billed under each object. If we permit to modify the data member at the time of creating a class then the data member conforming to all the objects which will possess the same initial value. Now we can find out that we are working with different initial values for different objects.

Practically in our programming segment we require that types of member which can automatically be called at the time of creating of an object to initialize its elements. If we want to create those types of method then we have to follow constructor procedure.

In our programming world constructor take a good role for faster service or action in the programming method and respective object.

Syntax to create a constructor:

Following is a generic syntax to create constructor.

Listing 1: Generic Syntax

 class <class name>
{
	<data member 1>
	<data member 2>
	<data member 3>
	<data member 4>
----------------------
---------------------- 
---------------------- 
----------------------
<data member t>
<class name>()
{
	<data member =value>
	<data member 2=value>
	<data member 3=value>
	<data member 4=value>
----------------------
---------------------- 
---------------------- 
----------------------
<data member t=value>
}
} 

NB: In the above syntax the italic and bolded part should always same.

Listing 2: Sample showing Example using constructor

class c1
{
	int x;
	double y;
	char z;
	string w;
c1()ß Default Constructor 
{
	x=0;
	y=0.0;
	z='c';
	s="Welcome to Computer Educational Institution...";
} 
}
  • In the above program we see that the class name is c1 and method name also c1() through this way we can know that this is constructor program though this constructor does not passing any value through the formal parameter.
  • Constructor also have parameterize and non-parameterize

Invoking a Constructor:

Constructor generally invoked by implicitly way. When we create a constructor then we have to call the following way.

Listing 3: Creating the constructor

class <class name>
{
		public static void main(String args[])//As concept of Java
		{
			 
			<class name> <object name> =new <class name>();
ß Call Constructor if it is default
			<class name> <object name> =new <class name>(value,value,value); 
ß Call Constructor if it is parameterized
		}
}

So, the above example we easily find out that if this constructor is easily called at the time of object creation. But if we use normal function in that place then we find that at first we have to create object after the creation of the object we are calling the method of the particular class. So it is sure that constructor is very first than normal function calling.

As well as we also find out those different types of impotency we can get from the using of constructor.

Some major advantages present in Constructor are following.

Advantages of Constructor:

  • We can easily remember about constructor. Because constructor name and the class name always same.
  • Constructor always directly invoked. As it is directly invoked its always use more first than normal method/function
  • No data type is used at the time of declaration of constructor.
  • Constructor can default or may parameterize.
  • Constructor support overloading concept. [When constructor name is same but it can pass different types of arguments then it is called constructor overloading.]
  • Opposite of constructor is destructor. Generally it is use at time of end of execution of the program.

List of Constructor

Default Constructor

Constructor without any arguments is called default constructor. Default Constructor is such types of constructor that have no parameter in the parameter list.

Object Supplied by Compiler

Listing 4: Sample showing Example using constructor by Compiler

	class cons1
{
		int i,j;
		cons1()
		{
			i=25;
			j=65;	
		}
		void show()
		{
			System.out.println("Value is"+i+"and"+j);	
		}
}
 

In the above sample Constructor is generate by the compiler.

Object Supplied by Programmers

Listing 5: Sample showing Example using constructor by Programmer

class cons1
{
		int i,j;
		cons1()
		{
			i=25;
			j=65;	
		}
		void show()
		{
			System.out.println("Value is"+i+"and"+j);	
		}
}
class c1
{
		public static void main(String args[])
		{
			cons1 ob=new cons1();
			ob.show();
		}
}

This is the Constructor program that generate by the programmer here we can generate constructor at first and call by the object ob.

Parameterized Constructor

Compiler Create Object

Listing 6: Sample showing Example using parameterized constructor by Compiler

class sri
{
	int x,y;
	sri(int m,int n)	
	{
		x=m;
		y=n;
	}
	sri(sri c1)
	{
		x=c1.x;
		y=c1.y;
	}
	void show()
	{
		System.out.println("Value is..."+x+" "+y);
	}
}

This is the parameterized constructor using this constructor we can easily send different types of data through the system.

This parameterize constructor also help full for communicate constructor overloading procedure.

Object created by programmer

Listing 7: Sample showing Example using constructor by Programmer

class sri
{
	int x,y;
	sri(int m,int n)	
	{
		x=m;
		y=n;
	}
	sri(sri c1)
	{
		x=c1.x;
		y=c1.y;
	}
	void show()
	{
		System.out.println("Value is..."+x+" "+y);
	}
}
class cei
{
	public static void main(String srimani[])
	{
		sri ob=new sri(100,200);
		ob.show();
		sri ob1=ob;//Initializing ob1 through ob
		ob.show();
	}
}

Concept of Copy Constructor:

Generally the constructor is used to copy the initial values of the instant variable of an object to the instant variable another object. Here constructor is also have some part one part is called Direct entry copy constructor and other part is called Copy constructor by passing object Copy constructor is the use for avoid bitwise copy. Because if we use bitwise copy it will copy data one by one and it is very slow then others.

The following example supports how to set copy constructor in our program.

Listing 8: Sample showing Example using Copy constructor

 class sri
{
	int x,y;
	sri(int m,int n)	
	{
		x=m;
		y=n;
	}
	sri(sri c1)
	{
		x=c1.x;
		y=c1.y;
	}
	void show()
	{
		System.out.println("Value is..."+x);
		System.out.println("Value is..."+y);
	}
}
class cons
{
	public static void main(String srimani[])
	{
		sri ob=new sri(100,200);
		System.out.println("Calling from the {ob} object.");
		ob.show();
		sri ob1=ob;//Initializing ob1 through ob
		System.out.println("Calling from the {ob1} object. which is copy");
		ob1.show();
	}
}

In the above program at first we create constructor and call it using ob [object] and this ob object copy to the ob1 and this ob1 object easily call the method of ob.

Concept of Constructor Overloading:

Constructor overloading is a concept where function name is same and arguments are different. For that process we can easily use only one constructor and that can execute more than one job.

Listing 9: Sample showing Example using constructor overloading.

consover.java
class sri
{
	int x,y;
	double z,PI=3.14;
	sri()	
	{
		System.out.println("Default Constructor......");
	}
	sri(int m,int n)	
	{
		x=m;
		y=n;
		int area=x*y;
		System.out.println("Area of Rectangle is......."+area);
	}
	sri(int m)	
	{
		x=m;
		int area=x*x;
		System.out.println("Area of Square is............."+area);
	}
	sri(double n)	
	{
		z=n;
		double area=PI*z*z;
		System.out.println("Area of Rectangle is.........."+area);
	}
}
class consover
{
	public static void main(String srimani[])
	{
		sri ob=new sri(); //Default Constructor
		sri ob1=new sri(100,200); //Two arguments are passing 
		sri ob2=new sri(121);//One arguments are passing
		sri ob3=new sri(85.99);//one double data types of arguments are passing
	}
}

In the above programming is best for successfully call different data types through same constructor. So it is one types of concept that support the phase “Single Interface Multiple Methods”

Differences between Constructor and a Member Method

Constructor Function/Method
1.2.Function name same as Class name Function name and class name same does not matter.
1.2.Calling procedure implicitly as well as explicitly. Calling procedure is explicitly.
1.2.Faster than function/methods. Slower that function/methods.
1.2.Concept of default constructor is present. No default function concept is present.
1.2.Opposite of constructor is called destructor. No opposite of function/method is present.
1.2.Constructor is also more secure than Function. Function is not more secure than constructor.
1.2.No Data type is used at the beginning of the constructor. Data types are needed at time of declaration of Function or method.

Conclusion:

The above scenario gives a bright picture of the implementation of constructor in our real life programming. Because if we are not using constructor then we have to face different types problem in our programming structure. So if we want to write our program and run this with standard format then no doubt constructor plays an important role in Software technology in object oriented programming.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login