Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using this in a class itself

 
marys joseph
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic