• Post Reply Bookmark Topic Watch Topic
  • New Topic

significance of this.someVar = value;  RSS feed

 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While trying to learn servlets and the such I have come across two ways of setting setter and getter methods. One of them involves the this. method and one does not. For example...





I have tested both and for what I'm doing they both work.
What is the significance of the 'this.'? Is it needed for a specific task down the road?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After digging around I found some stuff on using "this". I need to clarify what I think I understand...

I have a bean called GetResults().

What I'm trying to do is save a resultset to a bean. So I'm going about it in this way...

GetResults()-my bean class with setter and getter methods for my resultset fields.

I will put a loop in effect that will be like...


The way that I understand everything is that I should be using



since it will ensure that I get the correct instance? Or won't it actually matter?
[ November 27, 2007: Message edited by: Bryce Martin ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When there is no conflict, you can omit the this. prefix when referencing instance variables and Java figures out what you meant. Some people, myself included, always use this. when referencing instance variables (that way, the references are consistent regardless of where they take place and unintended conflicts during re-factoring are avoided). Others think it is an abomination. Either style is correct as far as Java is concerned.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. Since there is no difference I'll probably play it safe as I'm really new to all the java class stuff so I'll just use the "this." with all of them. Thanks for the point on refactoring.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!