CodeToConcept
About

java

java Tutorials

Introduction
Java Fundamentals
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Control Statements
Java Class & Object
java Method
Java Constructor
Java this keyword
OOP Principles
Java super keyword
Coming Soon Topics

java Tutorials

Introduction
Java Fundamentals
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Control Statements
Java Class & Object
java Method
Java Constructor
Java this keyword
OOP Principles
Java super keyword
Coming Soon Topics

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

Java

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

this keyword

  1. this keyword is reference variable or instance variable which holds the address of current object.

It is used inside methods and constructors which refer to the current object

Example:

class HelloWorld {
 
    public static void main(String[] args) {
        MyClass obj = new MyClass("Hello, Ashish...");
        obj.display(); // output : Hello, Ashish...
    }
}
 
class MyClass {
    String message; //instance variable
 
    HelloWorld(String message) {
        this.message = message; // here 'this' refers to current object's variable
    }
 
    void display() {
        System.out.println(this.message); // here 'this' refers to current object
    }
}
  1. It helps to distinguish between class variables and local variables when they have the same name.

Example:

class HelloWorld {
 
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.display();
        //output :
        // Local Variable: Hello from method!
        //Instance Variable: Hello from class!
    }
}
 
class MyClass {
    String message = "Instance Variable: Hello from class!"; // instance variable
 
    void display() {
        String message = "Local Variable: Hello from method!"; // local variable
 
        System.out.println(message);       // refers to local variable
        System.out.println(this.message);  // refers to instance variable
 
        //So here same variable name is used for both local and instance variables, 'this' helps to distinguish them.
    }
}
  1. In constructor chaining, when multiple constructors are present in a class, we can call one constructor from another using the this keyword

Example:

class HelloWorld {
 
    public static void main(String[] args) {
        MyClass obj = new MyClass(); // Creating object will call the no-argument constructor first
    }
}
 
class MyClass {
 
    // Constructor 1 (no-argument constructor)
    MyClass() {
        // 'this' keyword must be the first statement in the constructor
        this("Hello from parameterized constructor", 100); // this() is used to call constructor-2 of the same class
        System.out.println("No-arg constructor called");
    }
 
    // Constructor 2 (parameterized constructor)
    MyClass(String message, int number) {
        System.out.println("Parameterized constructor called");
    }
}
  1. Using this to call an instance method.

Example:

class HelloWorld {
 
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.show(); //output : Instance method called
    }
}
 
class MyClass {
 
    void display() {
        // instance method
        System.out.println("Instance method called");
    }
 
    void show() {
        // this.display() calls the instance method of current object
        this.display();
    }
}
  1. It cannot be used in a static context as it is associated with the object. Because static methods belong to the class, not objects.

Example:

class HelloWorld {
 
    public static void main(String[] args) {
        show(); 
    }
 
    static void show() {
        // this cannot be used here because show() method is static
        System.out.println(this); // ❌ Compilation error - non-static variable this cannot be referenced from a static context
    } 
}
  1. this() should be the first statement in a constructor.

Example:

class HelloWorld {
 
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
 
class MyClass {
 
    MyClass() {
        // this() must be the first statement in constructor
        this(10);  
 
        System.out.println("No-argument constructor");
    }
 
    MyClass(int x) {
        System.out.println("Parameterized constructor: " + x);
    }
}

See you in next page....🙂

← Java ConstructorJava Encapsulation →
← Java ConstructorJava Encapsulation →