• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

using this in a class itself

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)>
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic