BeginnersBook

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – parameterized constructor with example

By Chaitanya Singh | Filed Under: OOPs Concept

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.

Java Tutorial

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Java Date
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap