I am just a beginner in Java.I was trying this small this keyword program but caught up with error.
What is wrong in this below code?
I know we can directly print string name,but I want to use this keyword and then print.
Ernest Friedman-Hill wrote:That looks good. Note that since the name of the member variable ("name") and the name of the parameter ("str") are different, and there's no confusion, using "this" is actually optional. Your code is fine, but most people will just write "name = str;", without the "this" -- it means exactly the same thing. If you have a parameter or other local variable and a member variable with the same name, though, then "this" is required if you want to refer to the member variable.
Thank you so much.
I have now clearly understood the use of this keyword.