Coding/Learn/java/constructors
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnConstructors in Java
29 / 31 0%
Lesson 29 of 31
6 mins read

Constructors in Java

PreviousNext Lesson
In Plain English

A constructor is a special method that is called automatically when an object is created with `new`. Constructors have the exact same name as the class and have NO return type. They are used to initialize object fields with valid starting state.

The Mental Model

"A constructor is like the factory setup process for a new smartphone: as soon as it comes off the assembly line, the constructor installs the OS, sets the default serial number, and powers it on for the first time."

Deep Dive: How It Works

Rules: Constructor name MUST match class name exactly, and it must have NO return type (not even void).

The `this` keyword: Refers to the current object instance. `this.name = name;` resolves name collision between parameters and fields.

Default constructor: If you don't write any constructor, Java provides an empty no-arg constructor automatically. If you define a parameterized constructor, the default one is no longer provided unless you write it explicitly.

Syntax Blueprint

class Player {
    String name;
    int score;

    Player(String name, int score) {
        this.name = name;
        this.score = score;
    }
}

Use this.field = param to assign incoming constructor arguments to instance fields.

PointCore Rules to Remember

Point star
Automatic Invocation: Runs immediately when new ClassName(...) is executed.
Point star
The this Keyword: Disambiguates between instance fields and parameter names.
Point star
Constructor Overloading: Classes can have multiple constructors with different parameters.

Common Beginner Traps & How to Fix Them

Mistake:Adding a return type to constructor: void Player() { ... }

Why it happens: Adding void turns the constructor into a regular method; it will no longer initialize objects on new.

How to fix: Never include any return type on a constructor.

Pro Tip: Use constructor chaining with this(...) to reuse constructor initialization logic.

Live Interactive Example

Hit Run Code to see it live
Parameterized Constructor and this Keyword
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Output Console
Click "Run Code" to view the rendered output.
How it works: The constructor initializes host and port simultaneously during new ServerConfig(...).

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a Product constructor

  • PointIn the Product class, create a constructor Product(String name, double price).
  • PointAssign this.name = name and this.price = price.
  • PointIn main, create new Product("Keyboard", 49.99) and print "Product: " + p.name + " costs $" + p.price.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates