The moose likes Beginning Java and the fly likes using this in a class itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using this in a class itself " Watch "using this in a class itself " New topic
Author

using this in a class itself

marys joseph
Greenhorn

Joined: May 14, 2004
Posts: 20
I have been programming in Java for 3 years now, I recently moved to a new company and they are using this everywhere in the same class.

for ex:

public class MyClass{

public MyClass(){
}

public myMethodA(){
this.myMethodB(); //is this necessary?
}
public myMethodB(){

}
}

I remember I read somewhere we don't have to do this.. but I don't have anything to back it up and I am not even sure if I am right.. can someone throw some light?

thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's not necessary; it has no effect here one way or another. Your new employer's style guide may call for this, so why not ask someone to be sure.


[Jess in Action][AskingGoodQuestions]
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
it's not necessary in Java, but i like to do it myself too. it makes explicit that you're referring to an instance method/variable as opposed to a static method/variable; in the case of variables, it also distinguishes between locals and instance/class variables. i feel explicit is better than implicit, so i accept the extra verbosity of using the "unnecessary" this everywhere.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by M Beck:
it's not necessary in Java, but i like to do it myself too. it makes explicit that you're referring to an instance method/variable as opposed to a static method/variable; in the case of variables, it also distinguishes between locals and instance/class variables. i feel explicit is better than implicit, so i accept the extra verbosity of using the "unnecessary" this everywhere.


I used to use "warts" for this purpose: m_something for instance members, s_something for statics, no warts for locals. But now that IDEs have gotten smart enough to color these three in different colors, you can see at a glance what something is without using "this" or a "wart." I use yellow for locals, purple for members, and bright green for statics.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

For one, using 'this' explicitly, can help you avoid (sometimes) hard to find bugs like


Where you really meant



But as Ernest said, since any half decent IDE or Code Analysis tool will point out such bugs anyway, I dont know how valuable this practice is.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

I'm a large proponent of always using "this" simply because I like my code to read like sentences (and I come from Objective-C, where "self" is not optional)

In this manner, I say <subject>.<verb>(<direct object(s)>


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using this in a class itself