Skip to main content

this keyword

  • this is a keyword which acts as a reference variable.
  • This reference variable contains the address of the current object.
  • It is an instance reference variable and cannot be accessed from static context.

This keyword can be used in three ways

Consider the following class:

class Hello {
int a;
int b;

void m1() { ... }
void m2() { ... }

Hello(int a,int b) { ... }
Hello(int a) { ... }
Hello() { ... }
}

1) To access the variables

Syntax:

this.<variableName>

Example:

this.a;
this.b;

2) To access the methods

Syntax:

this.<methodName>();

Example:

this.m1();
this.m2();

3) To access the overloaded constructor

Syntax:

this(parameters);

Examples:

this();// invokes Default Constructor
this(99);// invokes 1-Arg Constructor
this(99, 88);// invokes 2-Arg Constructor

Important Notes

  • this always refers to the current object
  • Used to resolve ambiguity between instance variables and parameters
  • Cannot be used in static methods/blocks
  • this() is used for constructor chaining

Lab339.java

class Lab339 {

public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}

class Hello {

int a = 10;

void show() {
String a = "JavaWorld"; // local variable
System.out.println(a);
System.out.println(a);
}
}

Output:

JavaWorld
JavaWorld

👉 Local variable a overrides instance variable inside method.


Lab340.java

class Lab340 {

public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}

class Hello {

int a = 10;

void show() {
String a = "JavaWorld"; // local variable
System.out.println(a); // local
System.out.println(this.a); // instance
}
}

Output:

JavaWorld
10

👉 this.a refers to instance variable, not local.


Lab341.java

class Lab341 {

public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}

class Hello {

int a = 10;
static int b = 20;

void show() {
String a = "JavaWorld";
String b = "SD";

System.out.println(a); // local
System.out.println(b); // local
System.out.println(this.a); // instance
System.out.println(this.b); // static (via object reference)
}
}

Output:

JavaWorld
SD
10
20

👉 Priority: Local > Instance > Static


Lab342.java

class Lab342 {

public static void main(String[] args) {
Student stu1 = new Student(88, "Midhun");
stu1.show();

Student stu2 = new Student(99, "Balan");
stu2.show();
}
}

class Student {

int sid;
String sname;

Student(int sid, String sname) {
System.out.println("Student 2-Arg Constructor");
sid = sid; // problem
sname = sname; // problem
}

void show() {
System.out.println(sid + "\t" + sname);
}
}

Output:

Student 2-Arg Constructor
0 null
Student 2-Arg Constructor
0 null

👉 Reason: Local variables assigned to themselves → instance variables unchanged.


Lab343.java

class Lab343 {

public static void main(String[] args) {
Student stu1 = new Student(88, "Midhun");
stu1.show();

Student stu2 = new Student(99, "Balan");
stu2.show();
}
}

class Student {

int sid;
String sname;

Student(int sid, String sname) {
System.out.println("Student 2-Arg Constructor");
this.sid = sid; // correct
this.sname = sname; // correct
}

void show() {
System.out.println(sid + "\t" + sname);
}
}

Output:

Student 2-Arg Constructor
88 Midhun
Student 2-Arg Constructor
99 Balan

Key Concepts from This Page

🔹 Variable Priority

Local Variable > Instance Variable > Static Variable

🔹 Why this is needed?

When variable names are same:

this.sid=sid;

👉 Distinguishes:

  • Left → instance variable
  • Right → parameter (local variable)

🔹 Common Mistake

sid=sid;// wrong

✔ This does nothing useful


🔹 Correct Way

this.sid=sid;// correct

🧠 Interview Tips

👉 Q: Why use this keyword?

To refer to current object and resolve naming conflicts.

👉 Q: Can we access static variable using this?

Yes (but not recommended) → use class name instead.

👉 Q: What happens if this is not used?

Local variables override instance variables.

Lab344.java

class Lab344 {

public static void main(String[] args) {
Hello h = new Hello(99);
h.show();
}
}

class Hello {

int a;

Hello() {
System.out.println("Hello DC");
}

Hello(int a) {
this(); // calls default constructor
System.out.println("Hello 1-Arg Con");
this.a = a;
}

void show() {
System.out.println(a);
}
}

Output:

Hello DC
Hello 1-Arg Con
99

🧠 Concept:

  • this() is used for constructor chaining
  • Must be the first statement inside constructor

Lab345.java

class Lab345 {

public static void main(String[] args) {
Hello h1 = new Hello();
}
}

class Hello {

Hello() {
this(); // recursive constructor call
}
}

Result:

  • Compilation Error

🧠 Reason:

  • Constructor is calling itself → infinite recursion
  • Constructor chaining must not be cyclic

Lab346.java

class Lab346 {

public static void main(String[] args) {
Hello h1 = new Hello();
}
}

class Hello {

Hello() {
this(10); // calls parameterized constructor
}

Hello(int a) {
this(); // again calls default → recursion
}
}

Result:

  • Compilation Error

🧠 Reason:

  • Circular constructor calls:
    Hello() → Hello(int) → Hello() → ...
  • Leads to recursive loop → not allowed