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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Know About Parameterized Constructor In Java With Examples

Last updated on Jun 05,2023 132.7K Views

7 / 72 Blog from Java Core Concepts

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.

What are Constructors in Java | Types of Java Constructors | Java Tutorial | Java Training | Edureka

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.

Elevate your coding skills to new heights with our dynamic Full Stack Development Course.

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 or you can also join Java Training in Amravati.      

Elevate your web development skills with our industry-relevant Node JS Certification program and stay ahead in the ever-evolving tech world.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

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.

image not found!
image not found!

Know About Parameterized Constructor In Java With Examples

edureka.co