Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Know About Parameterized Constructor In Java With Examples

Published on Jul 03,2019 4.3K Views
7 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Java is one of the many programming languages which follows Object Oriented Approach. That means while programming in Java we have all the powerful features of Data Abstraction, Polymorphism, Inheritance etc. The core of all the OOP features is the implementation of Classes and Objects and their interaction with one another. In this article we will particularly look at how to initialize an object using parameterized constructors in Java. Please note that a basic understanding of classes and objects is required before you continue to constructors.

What Is A Constructor?

A constructor is basically a method that is automatically called when an object(instance) is created of that class. It is used to initialize an object’s data members.

public class Edureka{
Edureka(){ System.out.println("constructor is made");}
}

Some features of constructor include:

  1. It has the same name as the class name
  2. It has no return type

Types Of Constructor

  1. Default Constructor
  2. Parameterized Constructor

types of constructors-parameterized constructor in java-edureka

Default Constructor vs Parameterized Constructor

Default Constructor – A constructor that accepts no parameter is called Default Constructor. It is not necessary to have a constructor block in your class definition. If you don’t explicitly write a constructor, the compiler automatically inserts one for you.

Example illustrating Default Constructor in Java:

public class Edureka{
Edureka()
{ System.out.println("I am a constructor");}
public static void main(String args[]){
Edureka obj = new Edureka();
}
}
Output: I am a constructor

Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values.

Example illustrating Parameterized Constructor:

public class Edureka{
String studentName;
int studentAge;
//constructor
Edureka(String name, int age){
studentName = name;
studentAge = age;
}
void display(){
System.out.println(studentName+ " "+studentAge);
}
public static void main(String args[])
{
Edureka myObj = new Edureka("Manan" , 19);
myObj.display();
}
}
Output: Manan-19

In the above example, we are passing a string and an integer to the object. The constructor then initializes studentName and studentAge using the passed values. Display method then gives the desired output.

With a parameterized constructor for a class, one must provide initial values as arguments, otherwise, the compiler reports an error.

error-parameterized constructor in java- edureka

Passing Objects As Arguments

We can also pass arguments while creating other instances of a class. In this way parameterized constructors fulfills the need to copy one object’s values to another.

Example illustrating Passing Objects as Arguments:

public class Edureka{
String studentName;
Edureka(String name){
studentName = name;
}
Edureka(Edureka myObj){
this.studentName = myObj.studentName;
}
void display(){
System.out.println("Student:" + studentName);
}
public static void main(String args[])
{
Edureka obj1 = new Edureka("Manan");
/* passing the object as an argument for the constructor 
* this will invoke the copy constructor
*/
Edureka obj2 = new Edureka(obj1);
System.out.println("Printing obj1 - ");
obj1.display();
System.out.println("Printing obj2 - ");
obj2.display();
}
}
Output:
Printing object 1 - 
Manan
Printing object 2 - 
Manan

In the above example, we initialize obj1 using a string. We then pass obj1 as an argument while creating obj2. Finally, when we print both object’s studentName variable using display function we get “Manan”.

Calling Default Constructor From A Parameterized Constructor in Java

Sometimes there is a need to call the default constructor from another constructor of the same class. this keyword fulfills this purpose.

Example illustrating call to a default constructor from a parameterized constructor:

public class Edureka{
String studentName;
int studentAge;
String member;
Edureka(){
member = "YES";
}
Edureka(String name , int age){
this();
/* this is used for calling the default constructor
*from parameterized constructor
*/
studentName = name;
studentAge = age;
}
void display(){
System.out.println(studentName + " -" + studentAge+ "-"+ "Member" + member);
}
public static void main(String args[])
{
Edureka obj = new Edureka("Manan" , 21);
obj.display();
}
}

Output: Manan – 21 – Member YES

In the above example, when parameterized constructor in invoked, it first calls the default constructor with the help of this() keyword. The default constructor initializes “member” variable to “YES” and then continues to execute parameterized constructor.

Constructor Overloading

Constructor supports method overloading just like any other class. Based on different types or number of arguments, different constructors will be called.

 

Example illustrating Constructor Overloading:

public class Rectangle{
int length;
int breadth;
String color;
//constructor 1
Rectangle( int l , int b){
length = l;
breadth = b;
color = "Green";
}
//constructor 2
Rectangle(int l, int b, String c){
length = l;
breadth = b;
color = c;
}
void display(){
System.out.println("Length-" + length + "Breadth-" + breadth+ "Color" + color);
}
public static void main(String args[]){
Rectangle obj1 = new Rectangle(2,4);
Rectangle obj2 = new Rectangle(2,4,"Green");
obj1.display();
obj2.display();
}
}
Output:
Length - 2 Breadth - 4 Color - Green
Length - 2 Breadth - 4 Color - Red

Now that you have a grip on what constructors are and how to work with them, you are one step closer on your journey to learn Java. Concepts like constructors are simple but are extremely important as they involve classes and objects. For more in-depth topics and fun reads, enroll to Edureka’s Java certification program. Feel free to checkout our Java Tutorial blog to kickstart your learning.

Got a question for us? please mention this in the comments section of this ‘Parameterized Constructor in Java’ article and we will get back to you as soon as possible.      

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.