Skip to main content

Constructor Example in Java

2 min read Updated May 29, 2026
Share:
On this page (14sections)

Introduction

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

A constructor in a class is a special type of method called to create an object, It has the same name as that of the Class.

Constructor Types

  • Default Constructor
  • Parameterized Constructor
  • Private(SingleTon) Constructor

Constructor Characteristics In Java

  • A constructor has the same name of the Class
  • A constructor has no explicit return type
  • A constructor is called automatically, to create instance an object.
  • A constructor must be the same as the name of the class.
  • Not like methods, constructors are not members of a class. we cannot call directly
  • A constructor can be overloaded.
  • A Constructor with no argument is called default Constructor
  • A Constructor with arguments is called Parameterized Constructor
  • Private Constructor is used for Singleton Class

Syntax

NameOfClass() {
    //Statements
    }

Syntax Example

Class Constructor {
	Constructor(){
		System.out.print("Its Called");
	}
}

Syntax Explanation

  • In the example, the name of the constructor is the same as that of the class name. When the constructor is executed, “I am inside default constructor” will be printed.

Constructor Example Program

class Constructor {
	Constructor() {
		System.out.println("I am inside default constructor");
	}
 
	public static void main(String[] args) {
		Constructor obj = new Constructor();
	}
}

Sample Output

I am inside default constructor

When to use

Use this 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. A println / print call writes text to the console — part of the sample output below.

  3. Constructor obj = new Constructor(); updates a variable used in the calculation or output.

  4. Compare your console output with the sample output for Constructor to confirm the program behaves correctly.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — 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 .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Constructor program demonstrate?
It shows how to implement 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