• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance variables  RSS feed

 
Vlada Den
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which way is the best to set instance variables -

like this:

this.price = price;

or like this:

price = aPrice;
?

Thanks
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's a matter of personal taste. Some people don't like having a parameter that hides an instance variable because it might lead to errors or makes things less readable. Personally I don't think anyone looks at my code without an IDE that will make it visibly obvious and find having to change the parameter name to something like aPrice or pPrice annoying, so I don't.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc, that's not actually addressing the question. The OP is talking about this:



In the case where "this" is used it is used because it has to be used, it would be erroneous not to. Bruce Eckel is referring to people (and I know one) who obsessively use "this" even when it's completely superfluous:



Which is more or less a third alternative to what the OP is asking about, and very silly in my never to be humble opinion.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okey, nothing happend.
Stop talkin about that.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
marc, that's not actually addressing the question...

Right. I realized this immediately after posting, then removed my post. I guess I wasn't fast enough.

For anyone wondering: I misinterpreted the original question to be asking whether it's preferable to use "this" in situations where it's already implicit. (Some people think it adds clarity. But not me, and I quoted Bruce Eckel to explain why.)

However, in the situation that's actually being described here, I would definitely use a local variable of the same name as an instance variable, then use "this" (as you must) to access the instance variable.

Sorry for any confusion.
[ May 03, 2006: Message edited by: marc weber ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!