DEV Community

Ajay Sundar
Ajay Sundar

Posted on

Understanding Java Constructors and the this Keyword — Step‑by‑Step

  • This() refers to current object.

  • It is a constructor call in java

  • this() acts as a current class constructor and is used in parameterized constructor

  • used to call or execute a current class method

  • using this with a attribute x

public class Main {
int x;


// Constructor with a parameter
public Main(int x) {
this.x = x;
}


// Call the constructor
public static void main(String[] args) {
Main myObj = new Main(5);
System.out.println("Value of x = " + myObj.x);
}
}
Enter fullscreen mode Exit fullscreen mode

Step by Step Explanation

1) public class Main { — The class declaration

  • A class is a blueprint for objects. Main is the class name.

  • public means the class is visible to code in other packages.

2) int x; — Instance field (aka member variable)

  • x is an instance field of type int. Every Main object will have its own x.

  • Default value for an int field is 0 until you explicitly assign something.

3) public Main(int x) { ... } — Constructor with a parameter

  • This is a constructor: a special method that runs when you create an object with new.

  • Rules for constructors:

             - Same name as the class (Main).
             - No return type (not even void).
             - Can be overloaded (you can have multiple constructors with different parameter lists).
    
             - The parameter list (int x) declares a local variable x that exists only during the constructor call.
    

4) this.x = x; — Why this is essential here

x (the parameter) shadows the field x (they have the same name). Without this, references to x inside the constructor refer to the parameter, not the field.

this.x explicitly refers to the instance field x that belongs to the current object.

So this.x = x; reads: assign the value of the parameter x to the object's field x.

If you wrote x = x;, you'd simply assign the parameter to itself and the field would remain unchanged.

5) public static void main(String[] args) { — Program entry point

  • main is the method the JVM calls to start your program.

  • static means it belongs to the class, not to any particular object. That’s why you can call it without creating an instance first.

  • String[] args receives command‑line arguments (not used here).

6) Main myObj = new Main(5); — Object creation and constructor call

  • new Main(5) does two things:
  1. Allocates memory on the heap for a new Main object and initializes fields to default values (e.g., x = 0).

  2. Calls the constructor Main(int x) with the value 5.

Inside the constructor, this.x = x; sets the object's field x to 5.

The reference to the newly created object is stored in the variable myObj.

7) System.out.println("Value of x = " + myObj.x); — Printing the value

  • System.out.println(...) prints text to the console followed by a newline.

  • "Value of x = " + myObj.x concatenates the string with the integer value of myObj.x.

  • Final output on the console: Value of x = 5

Top comments (0)