Java Constructors

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 28 / 50 · English

Java Constructors

Create an object in a useful starting state.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Constructors.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

A constructor initializes a newly created object. Its name matches the class name and it has no return type. Use parameters to require the information an object needs from the beginning.

02 / Step by step

Explore the details

Java Constructors

A constructor is a special method that runs automatically when an object is created with new. It sets the object's initial state.

Constructor Syntax

Code anatomy / கோடின் விளக்கம்

CodeMeaning
CarConstructor name must exactly match the class name
StringParameter type
brandParameter — used to initialise the attribute
) {No return type — not even void
thisthis refers to the current object being created

The constructor name must match the class name exactly and has no return type. If you don't write one, Java silently provides an empty default constructor. Use this.field = param when the parameter name matches the field name.

Java reference fragmentJAVA
class Person {
    String name;
    int age;

    // Constructor
    Person(String name, int age) {
        this.name = name;  // 'this' = current object
        this.age = age;
    }

    void greet() {
        System.out.println("Hi, I'm " + name + ", age " + age);
    }
}

Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);
p1.greet();  // Hi, I'm Alice, age 25
p2.greet();  // Hi, I'm Bob, age 30
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        Learner learner = new Learner("Kavi", 3);
        System.out.println(learner.name + ": " + learner.level);
    }
}
class Learner {
    String name; int level;
    Learner(String name, int level) { this.name = name; this.level = level; }
}
Predict first. Then reveal the expected output / வெளியீடு
Kavi: 3

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Construct another Learner with your chosen name and level.

Reveal the answer and reasoning / விடை

Pass the two values to new Learner(name, level). Each object stores its own constructor arguments.

One mistake to avoid: void Learner(…) is a method, not a constructor. Once you declare a constructor, Java does not automatically add a no-argument constructor.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.