Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
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:
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.
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”.
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 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.
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.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co