JVM Architechture

This keyword in java with example

This keyword in Java

This keyword is used inside a method or constructor of a class. And it acts as a reference to the current object of the class, whose method or constructor is being invoked.

This keyword is used:

  • To represent Instance variables of current class.
  • To refer Methods of current class.
  • To Invoke Constructor of current class.
  • To return the current class instance.

Notes:

  •  While using inside a constructor this() statement must be a first statement of constructor otherwise compiler will raise compilation error and only one this() statement is allowed.
  •  In a  constructor either this() statement is  allowed or super(), both are not allowed.

1. this statement: to represent the current class instance variable

The this keyword can be used to represent current class instance variable.

If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

So to understand the problem of ambiguity, first we will see a program where this keyword is not used.

[pastacode lang=”java” manual=”class%20Employee%7B%20%20%0Aint%20empNo%3B%20%20%0AString%20name%3B%20%20%0Afloat%20salary%3B%20%20%0AEmployee(int%20empNo%2CString%20name%2Cfloat%20salary)%7B%20%20%0AempNo%3DempNo%3B%20%20%0Aname%3Dname%3B%20%20%0Asalary%3Dsalary%3B%20%20%0A%7D%20%20%0Avoid%20show()%7BSystem.out.println(empNo%2B%22%20%22%2Bname%2B%22%20%22%2Bsalary)%3B%7D%20%20%0A%7D%20%20%0Apublic%20class%20TestThis%7B%20%20%0Apublic%20static%20void%20main(String%20args%5B%5D)%7B%20%20%0AEmployee%20e1%3Dnew%20Employee(301%2C%22Alex%22%2C10000f)%3B%20%20%0AEmployee%20e2%3Dnew%20Employee(303%2C%22Sam%22%2C6000f)%3B%20%20%0Ae1.show()%3B%20%20%0Ae2.show()%3B%20%20%0A%7D%7D%20%20″ message=”Problem without using this statement” highlight=”” provider=”manual”/]

Output of the above code:

0 null 0.0
0 null 0.0

In the above example, the parameters and the instance variables are same, so due to ambiguity we are not getting any output.

Now we will resolve the problem using this keyword and  to distinguish local variable and instance variable.

[pastacode lang=”java” manual=”String%20name%3B%20%20%0Afloat%20salary%3B%20%20%0AEmployee(int%20empNo%2CString%20name%2Cfloat%20salary)%7B%20%20%0Athis.empNo%3DempNo%3B%20%20%20%20%20%20%2F%2Fwe%20have%20used%20this%20keyword%20now%0Athis.name%3Dname%3B%20%20%0Athis.salary%3Dsalary%3B%20%20%0A%7D%20%20%0Avoid%20show()%7BSystem.out.println(empNo%2B%22%20%22%2Bname%2B%22%20%22%2Bsalary)%3B%7D%20%20%0A%7D%20%20%0Apublic%20class%20TestThis%7B%20%20%0Apublic%20static%20void%20main(String%20args%5B%5D)%7B%20%20%0AEmployee%20e1%3Dnew%20Employee(301%2C%22Alex%22%2C10000f)%3B%20%20%0AEmployee%20e2%3Dnew%20Employee(303%2C%22Sam%22%2C12000f)%3B%20%20%0Ae1.show()%3B%20%20%0Ae2.show()%3B%20%20%0A%7D%7D%20%20″ message=”Resolving above problem using this keyword” highlight=”” provider=”manual”/]

Output of the above program:

301 Alex 10000.0
303 Sam 12000.0

 

Leave a Reply