A Constructor with arguments(or you can say parameters) is known as Parameterized constructor. As we discussed in the Java Constructor tutorial that a constructor is a special type of method that initializes the newly created object.
Example of Parameterized Constructor
We can have any number of Parameterized Constructor in our class. In this example, I have implemented four constructors: one is default constructor and other three are parameterized.
During object creation the parameters we pass, determine which constructor should get invoked for object initialization. For example, when we create the object like this
MyClass obj = new MyClass(123, "Hi");
then the new keyword invokes the Parameterized constructor with int and string parameters (MyClass(int, String)) after object creation.
class Example{ //Default constructor Example(){ System.out.println("Default constructor"); } /* Parameterized constructor with * two integer arguments */ Example(int i, int j){ System.out.println("constructor with Two parameters"); } /* Parameterized constructor with * three integer arguments */ Example(int i, int j, int k){ System.out.println("constructor with Three parameters"); } /* Parameterized constructor with * two arguments, int and String */ Example(int i, String name){ System.out.println("constructor with int and String param"); } public static void main(String args[]){ //This will invoke default constructor Example obj = new Example(); /* This will invoke the constructor * with two int parameters */ Example obj2 = new Example(12, 12); /* This will invoke the constructor * with three int parameters */ Example obj3 = new Example(1, 2, 13); /* This will invoke the constructor * with int and String parameters */ Example obj4 = new Example(1,"BeginnersBook"); } }
Output:
Default constructor constructor with Two parameters constructor with Three parameters constructor with int and String param
Example 2: Parameterized constructor – A weird compilation error
While discussing default constructor, we have seen that when we do not create a default constructor, Java compiler inserts the default constructor into the code on our behalf. However this is not always true. See the example below:
class Example{ Example(int i, int j){ System.out.print("parameterized constructor"); } Example(int i, int j, int k){ System.out.print("parameterized constructor"); } public static void main(String args[]){ Example obj = new Example(); } }
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Example() is undefined
The program throw compilation error because the statement Example obj = new Example();
is trying to invoke the no-arg constructor (no argument constructor) which we don’t have in our program. You don’t get this error if you remove the parameterized constructors from the above code. This is because if you do not have any constructor in your class, Java compiler inserts default constructor into your code on your behalf however if you implement any constructor then you no longer receive a default constructor.
Leave a Reply