BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – parameterized constructor with example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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.

Top Related Articles:

  1. Final Keyword In Java – Final variable, Method and Class
  2. What is private Keyword in Java
  3. Method Overloading in Java with examples
  4. Constructor Overloading in Java with examples
  5. What is new Keyword in Java

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • 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 Flow Control

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

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • 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 Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap