Default and Parameterized Constructor Example in Java
On this page (10sections)
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
//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
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
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
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
DefaultConstructorExample object1 = new DefaultConstructorExample();updates a variable used in the calculation or output. -
ParameterizedConstructorExample object2 = new ParameterizedConstructorExample(100,200);updates a variable used in the calculation or output. -
int num1 = 5, num2 = 10, result;updates a variable used in the calculation or output. -
result = num1+num2;updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
this.num1 = num1;updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Name classes in PascalCase and follow one public class per file when starting out.
- Keep
mainshort — delegate work to other methods as programs grow.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.