• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

When to use this keyword?

 
Ranch Hand
Posts: 263
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought in this situation:



shouldn't this line:


be


to indicate the use of the instance variable?
or is that now a local variable in the method? though I wouldnt think so because its type would have to be declared

I really should know this, i know.

 
Marshal
Posts: 6974
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are those, which are defined within the method (could be constructor also).

Other variables are either class (marked as static) or instance.

keyword 'this' refers to a this instance's variable.

In your particular example you gave, you don't need to prefix with 'this' because you don't have variable with the same name in local scope, meaning variable with same name in that particular method, however, adding 'this' sometimes adds clarity, no need to over-use though. Sometimes to add 'this' is mandatory so your assignment behaves intentionally. And such example is:


So in this case you say, save local (the one on the right) variable's "name" value to an instance (on the left) variable of the same name.

So what would happen if you weren't use 'this' in the code example:

You simply would re-assign passed as an argument variable's name value to itself AND object's instance variable would stay as it never been touched, that only happens because you have same variable name in 2 scopes (local and instance).
 
Saloon Keeper
Posts: 6049
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" is only needed to disambiguate a piece of code.  That is, a methods parameter can have a name that exactly matches a field name. Within the method the parameter will be used unless explicitly told not to by using "this". A typical example is a setter.
On the other hand, if you name the parameter something else then "this" is no longer required.
The first example is preferred.

Same goes for "this" in front of a method call. Only use "this" if the usage is ambiguous.
 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





this is reference to current object so if you have name conflicts between instance variable and formal parameters then you can use to distinguish between
 
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:"this" is only needed to disambiguate a piece of code. . . .

But there are some people who write this.xxx or this.xxx(...) for every mention of an instance field or method. Eclipse has an option to add this. to all instance fields or method calls automatically. As Carey says, it is redundant 99% of the time. I think you should always give your local variables different names from all your fields, so the issue doesn't arise. The exception is parameters in setXXX(...) methods (if any) and constructors, where the form Carey showed with this.xxx is preferred. The reason it is better is that you are exposing the well‑chosen field name rather than a naff name like nameIn to your users.

I am taking a neutral stance about this.xxx.
 
Saloon Keeper
Posts: 3415
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There iis also a practical advantage of using 'this', whether the use of it is necessary or not. That advantage is, that when using an IDE, typing "this." will make a window pop up with all the possibilities. And that can save you a lot of typing! For instance, in the constructor of a custom JPanel, when you use 'this.' then that window will let you click all that you may need, like 'setPreferredSize, setBackgtound, et cetera.
 
wayne brandon
Ranch Hand
Posts: 263
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all very interesting.

sorry Campbell with regards to this

" The reason it is better is that you are exposing the well‑chosen field name rather than a naff name like nameIn to your users. "

I just dont understand what you mean here, can you please explain
 
Liutauras Vilda
Marshal
Posts: 6974
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:Thanks all very interesting.

sorry Campbell with regards to this

" The reason it is better is that you are exposing the well‑chosen field name rather than a naff name like nameIn to your users. "

I just dont understand what you mean here, can you please explain


I'm not Campbell, but I think he meant, 'this' is useful in particular when you define a well thought through name, for instance in setter:
As opposed to scratching your head how to name method's parameter differently (most challenging - not worse), so it wouldn't clash with instance variable.

Worse:
 
Bartender
Posts: 10775
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:I just dont understand what you mean here, can you please explain


One classic case is when you have a constructor with parameters. Usually those parameters will be used to set instance fields in your class, so it makes sense to give them the same name, and you'll end up with something like:

Note that the 'this' is absolutely required, because otherwise the compiler can't tell whether you're referring to the parameter field or the instance field (and it will assume the first because it's "closest"), so line 7 says
"assign the parameter field 'firstName' to the instance field 'firstName'".

Hope it helps.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!