• Post Reply Bookmark Topic Watch Topic
  • New Topic

The "this" keyword in Java  RSS feed

 
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings fellow programmers,

I am preparing for the OCA Java SE 8 Programmer I exam and I am on the objective Reading and Writing Object Fields. This objective lead me to the "this" keyword which I was a little confused about. Please explain to me. It would also be great if you could explain the objective more clearly!

Thanks,

Maxwell
 
Greenhorn
Posts: 16
3
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" is a reference variable that refers to the current object. There are lots of usage of "this" keyword, most commons are,
  • The "this" keyword can be used to refer current class instance variable.
  • If there is instance variable and local variable with the same name, "this" keyword used to refer instance variable.


    in above code example we can not refer instance variables x and y without using "this". Local variables x and y in constructor shadows instance variables x and y in the scope of constructor. That is why when we refer x or y in constructor code will use the local variables, and to refer to the x and y field of Point, the constructor must use this.x and this.y.

  • The this() can be used to inside the constructor to call another overloaded constructor in the same class .
  • This occurs if a class has overloaded constructors, for exm: one without argument and another with two int argument.


    These are enough for OCA Java SE 8 Programmer I exam, if you want more you can find another usage of "this" by searching on the internet.
     
    Ranch Hand
    Posts: 116
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Im not really experienced programmer, I would just declare values in this way.
    Is it true that 'this.' can be used only in constructor?     'this.' cannot be used in method?
     
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    bob john wrote:
    Is it true that 'this.' can be used only in constructor?     'this.' cannot be used in method?


    It would take only a minute to try it out.... although, as a quick note, make sure your method is not declared as static.

    Henry
     
    Mehman Ismayilov
    Greenhorn
    Posts: 16
    3
    Android Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    bob john wrote:
    Is it true that 'this.' can be used only in constructor?     'this.' cannot be used in method?

    in adition to legal usage of "this" and this(), remember "this" can only be used in instance methods and constructors. Because static methods can be called with class name and in this situation there is no object that "this" will refer, "this" can not be used in static methods. this() can only be used in constructors as the first statement.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!