Contact Us || Privacy Policy || About Us |
|
this keyword in java→ this keyword is used to refer to the current object inside a method or a constructor etc. Usage of this keyword.
1) this keyword used in Instance variable→ this keyword can be used to refer current class instance variable. Example 1 : by this keyword Output ![]() Example 2 : problem without this keyword Output ![]() 2) this keyword used in Method→ this keyword can be used to invoke the method of the current class.If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Example : Output ![]() 3) this keyword used in Constructor→ this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining. Example : Output ![]() 4) Pass as an argument in the method→ this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Example : Output ![]() 5) Passed as argument in the constructor call.We pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Example : Output ![]() 6) Return the current class instance from the method..We can return this keyword as an statement from the method. Syntax : Example : Output ![]()
Next TopicInheritance in Java
|