Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

No-data

  • 3 years ago

Hi, I'm having troubles understanding the "this" keyword in Java. I already googled it and red the Java tutorial on that plus some other webpages. It says: "The most common reason for using the "this" keyword is because a field is shadowed by a method or constructor parameter". I don't quite get the meaning of "is shadowed by ...". Could someone help me please?

  • This Question is Closed
  1. agdgdgdgwngo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    The `this' keyword is basically an alias for the current object that invoked the method. When you are writing some class methods and you need to mutate variables that are local to an instance of that class, you may use `this' to refer to that particular instance/object.

  2. agdgdgdgwngo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Oh and this can be used to call class constructors of an object like this().

  3. No-data
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I understood the last thing and it's pretty cool. What do you mean by "mutate variables that are local to an instance of that class"?

  4. No-data
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I know that a local variable is a variable that is inside a method and that an instance variable is outside any method. But I think that is not enough for me to understand =/.

  5. agdgdgdgwngo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    exactly; when you create a bunch of objects, each of them has their own variables.

  6. No-data
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Ok maybe I could understand better if I knew what would happened if the "this" keyword would not exist?

  7. agdgdgdgwngo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    without the this keyword, an object can't refer to itself and its own fields in its methods

  8. No-data
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Hmmm I think I got it. Thank you so much @agdgdgdgwngo you're awesome!

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy