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?
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.
Oh and this can be used to call class constructors of an object like this().
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"?
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 =/.
exactly; when you create a bunch of objects, each of them has their own variables.
Ok maybe I could understand better if I knew what would happened if the "this" keyword would not exist?
without the this keyword, an object can't refer to itself and its own fields in its methods
Hmmm I think I got it. Thank you so much @agdgdgdgwngo you're awesome!
Join our real-time social learning platform and learn together with your friends!