Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

The ‘this’ Keyword

• In Java the word this is a keyword.
• Actually, it is a Final Instance Type Reference Variable.
• Since this keyword in a final type reference variable, so we can’t change its value.
• We cannot access this keyword from any static context (static block | static method) because as we know that all the static contexts work on early-binding concept whereas, this keyword is an instance type reference variable.
• As we know a reference variable contains an object reference and because this keyword is a reference variable so it holds current working object reference.

            
class JTC{
	public static void main(String arg[]){
		System.out.println("---This is Main Method of JTC class---");
		A a1 = new A();
		System.out.println("\nHashCode of a1 :- "+a1.hashCode());
		a1.m1();
		A a2 = new A();
		System.out.println("\nHashCode of a2 :- "+a2.hashCode());
		a2.m1();
	}
}
class A{
	void m1(){
		System.out.println("---m1() in A class---");
		System.out.println("HashCode of this :- "+this.hashCode());
	}
}


        
    ---This is Main Method of JTC class---

    HashCode of a1 :- 37380050
    ---m1() in A class---
    HashCode of this :- 37380050

    HashCode of a2 :- 2023938592
    ---m1() in A class---
    HashCode of this :- 2023938592
            

In the above example we have a class which is A where we have declared an Instance Method which is void m1() and from JTC class we are invoking m1() along with two different objects of A class which are A a1 = new A() and A a2 = new A(). As we know that every object contains its unique hash code, so we get the hash code of a1 and a2 from main method and when we invoke m1() with A a1 = new A()we get the same hash code from m1() and the same happens when we invoke m1 with A a2 = new A(). This proves that this keyword always contains the reference of the current working object.

            
class JTC{
	public static void main(String arg[]){
		System.out.println("----Main Method in JTC class----");
		Student s1 = new Student(101,"Vivek","Noida");
		s1.show();
	}
}
class Student{
	int sid;
	String sname;
	String scity;
	
	Student(int sid, String sname, String scity){
		sid = sid;
		sname = sname;
		scity = scity;
	}
	void show(){
		System.out.println("\n----Student Details----");
		System.out.println("Sid :- "+sid);
		System.out.println("Sname :- "+sname);
		System.out.println("Scity :- "+scity);
	}
}

        
    ----Main Method in JTC class----

    ----Student Details----
    Sid :- 0
    Sname :- null
    Scity :- null
            

In this program we are creating an object of Student class and trying to inject the data into the class level fields sid, sname and scity using Student class constructor but in Student class constructor which is Student(int sid, String sname, String scity) we have parameters and their names are same as class level fields . That is the reason that when we are assigning the values which are sid = sid, sname = sname and scity = scity the values are not getting stored in the class level fields, the values get stored into the local variables and we get the default value in output as the value of sid, sname and scity.

            
class Student{
	int sid;
	String sname;
	String scity;
	
	Student(int sid, String sname, String scity){
		this.sid = sid;
		this.sname = sname;
		this.scity = scity;
	}
	void show(){
		System.out.println("\n----Student Details----");
		System.out.println("Sid :- "+sid);
		System.out.println("Sname :- "+sname);
		System.out.println("Scity :- "+scity);
	}
}
class JTC{
	public static void main(String arg[]){
		System.out.println("----Main Method in JTC class----");
		Student s1 = new Student(101,"Vivek","Noida");
		s1.show();
	}
}


        
    ----Main Method in JTC class----

    ----Student Details----
    Sid :- 101
    Sname :- Vivek
    Scity :- Noida
            

The above example is almost same as the previous example, there is only a difference in the Student class constructor. We are initializing class level variables along with the this keyword (this.sid = sid | this.sname = sname | this.scity = scity) which represents the current working object i.e. Student s1 = new Student(). It means this.sid is equivalent to s1.sid; this.sname is equivalent to s1.sname; and this.scity is equivalent to s1.scity. That is the reason the data get stored inside the class level variable and we get their corresponding values in the output.