Skip to main content
Browse topics

Default and Parameterized Constructor Example in Java

2 min read Updated May 29, 2026
Share

Introduction

Default and Parameterized Constructor is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

Default Constructor: A Constructor with no argument is called default Constructor
Parameterized Constructor: A Constructor with arguments is called Parameterized Constructor

Syntax

java
//Default Constructor
Class ConstructorExample {
	ConstructorExample (){
		//Do something
	}
}

//Parameterized Constructor
Class ConstructorExample {
    <datatype> variableName;

	ConstructorExample (<datatype> variableName){
            this.variableName = variableName;
		//Do something
	}
}

Default and Parameterized Constructor Example Program

java
public class DefaultAndParameterizedConstructor {
    public static void main(String[] args) {
        //Creating object of class using default constructor
        DefaultConstructorExample object1 = new DefaultConstructorExample();
        object1.doAddition();
        
        //Creating object of class using parameterized constructor
        ParameterizedConstructorExample object2 = new ParameterizedConstructorExample(100,200);
        object2.doAddition();
    }
}

class DefaultConstructorExample{
    int num1 = 5, num2 = 10, result;
    int doAddition(){
        result = num1+num2;
        System.out.println("This method is called using a default constructor");
        return result;
    }
}

class ParameterizedConstructorExample{
    int num1, num2, result;

    public ParameterizedConstructorExample(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }
    
    int doAddition(){
        result = num1+num2;
        System.out.println("This method is called using a parameterized constructor");
        return result;
    }
}

Sample Output

plaintext
This method is called using a default constructor
This method is called using a parameterized constructor

When to use

Use this default and parameterized constructor example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. DefaultConstructorExample object1 = new DefaultConstructorExample(); updates a variable used in the calculation or output.

  3. ParameterizedConstructorExample object2 = new ParameterizedConstructorExample(100,200); updates a variable used in the calculation or output.

  4. int num1 = 5, num2 = 10, result; updates a variable used in the calculation or output.

  5. result = num1+num2; updates a variable used in the calculation or output.

  6. A println / print call writes text to the console — part of the sample output below.

  7. this.num1 = num1; updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

Common Mistakes

Frequently Asked Questions

What does the Default and Parameterized Constructor program demonstrate?
It shows how to implement default and parameterized constructor in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related tutorials

Search tutorials