This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes this operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this operator" Watch "this operator" New topic
Author

this operator

tom chansky
Greenhorn

Joined: Nov 01, 2004
Posts: 28
Are there any major performance issues in using the "this" operator in a class?

For example:

Public Class MyObject {
private String title;

public void setTitle(String title){
this.title = title;
}



}
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
i very seriously doubt that this style of writing would slow your code down at all - it seems to me like something the compiler should be able to very easily identify and optimize for. but even if it does, i'd still do it; you gain so much more clarity about what kind of variable you're using, your code gets so much more easily readable, that i think it would be worth it.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

As opposed to what? This code?

It's exactly the same thing. In this case the "this" reference is implied. Even if there were a difference, in any non-trivial program the difference certainly would be dwarfed by GUI code, IO overhead, network latency and so on.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
If you're hiding a variable name, which you're doing here by using the same name in the parameter list as in the class, you need to use this to remove ambiguity.

For example, the following code prints correctly:


The output is "Testing 1-2-3", as expected. However, if we remove the "this" keyword, as in the following code:


The output here is "null". Why? Because Java tried to assign "this.title" to "this.title" because it didn't know which "title" the assignment wanted to use for the right value!

By the way, the original code also had two capitalization errors, "Public" for "public" and "Class" for "class". Java cares about such things.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The choice of variable names does not affect execution times. The choice of method names might do this if it results in dynamic dispatching (polymorphism), but variable references are resolved at compile time.

[ February 25, 2005: Message edited by: Mike Gershman ]
[ February 25, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jeff Bosch:
If you're hiding a variable name, which you're doing here by using the same name in the parameter list as in the class, you need to use this to remove ambiguity.

For example, the following code prints correctly:


The output is "Testing 1-2-3", as expected. However, if we remove the "this" keyword, as in the following code:


The output here is "null". Why? Because Java tried to assign "this.title" to "this.title" because it didn't know which "title" the assignment wanted to use for the right value!

By the way, the original code also had two capitalization errors, "Public" for "public" and "Class" for "class". Java cares about such things.

Actually, the parameter named "title" overrides the member variable named "title". So in the later example, "title=title" assigns the value of the parameter to itself. However, when the method returns, the memory where the parameter is stored is destroyed, so the assignment essentially did nothing.

Layne


Java API Documentation
The Java Tutorial
 
Consider Paul's rocket mass heater.
 
subject: this operator
 
Similar Threads
Listing >> OOCalculator >> Ilja
help with loop
cannot show String
Object Reference Not Initialized
Selecting a value in a combo box that equals the value from the database record?