Skip to content
José da Cruz

Enterprise Architect, and Life Thinker

José da Cruz

Enterprise Architect, and Life Thinker

Java Learning: Default Constructor

josedacruz, January 3, 2024

The objective of a constructor is to create an object from a class blueprint.
Since the main idea of this action is to create and initialize an object, it makes sense to use the class’s name as the method’s name.
This is a special method since it has no explicit return type. I say explicit because it returns a reference to the object created and allocated in the heap. We could say it returns this, the reserved word that can be used inside the class to reference the current object. Some languages call it self, which seems very appropriate.
Another distinction from other method calls is the word new; this is how the compiler knows we are targeting a constructor method and not a local method.

BankAccount ba = new BankAccount(); // OK: the word new signals to choose a constructor method

BankAccount ba = BankAccount(); // ERROR: Will try to call a local method

All classes should have at least one constructor, meaning you should have a method with no return type and with the same class’s name.
Most of the classes don’t need, at the start, specific logic or values to be initialized when the object is created. To avoid always writing an empty constructor, if none is provided, the Java compiler assumes a default constructor exists.

class BankAccount {
	private String customerId;
	private String customerName;

	// optional
	public BankAccount() {}
}

class SavingsAccount {
	// There is an empty constructor here!
}


public class Finances {
	public static void main(String... args) {
		BankAccount ba = new BankAccount();
		SavingsAccount sa = new SavingsAccount();
	}
}

Related

java java

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

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

  • architecture (154)
  • artificial-intelligence (6)
  • books (2)
  • courses (4)
  • decision-frameworks (4)
  • finances (1)
  • java (8)
  • life-improvement (20)
  • metrics (3)
  • observability (2)
  • puzzles and challenges (3)
  • reviews (1)
  • security (8)
  • spring-boot (36)
  • Systems Thinking (3)
  • GitHub Weekly: MCP Tools Trending Right Now (September 28 – October 4, 2026)
  • The Bulkhead Pattern: Why One Slow Dependency Shouldn’t Sink the Whole Ship
  • Essential Reading: A Philosophy of Software Design — Fighting Complexity One Module at a Time
  • GitHub Weekly: Top 10 Trending Repos (September 21 – September 27, 2026)
  • The Volunteer Tax: Why Raising Your Hand in a Meeting Makes You the Permanent Owner
©2026 José da Cruz | WordPress Theme by SuperbThemes