Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

No-data

  • 2 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
    • 2 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
    • 2 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
    • 2 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
    • 2 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
    • 2 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
    • 2 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
    • 2 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
    • 2 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.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.