26

I have seen some programmers using _ (underscore) in front of class names, and others using it for local variables.

Does the Java standard require/suggest the use of _ (underscore) in front of an private instance variable or class name?

1
  • 9
    Disagree with the close - Java is one of the languages which has an official style defined, so a question regarding whether the standard addresses a point seems reasonable. Jun 18, 2014 at 9:45

10 Answers 10

43

I think artifacts like these are pre-IDE and C++ vintage. Don't do it.

5
  • Can't imagine why this answer, and only this answer, was voted down five years later.
    – duffymo
    Jan 3, 2015 at 0:09
  • Just voted up for fixing the justice. Edit: I'm not the guy who voted down Jan 4, 2015 at 21:14
  • and because your answer is the most useful one for me, I'll vote up
    – amal50
    Nov 9, 2015 at 23:54
  • 3
    Just because it's "pre-IDE and C++ vintage", we won't do it? IDE's give the advantage but it won't help me from renaming stuff when a private member gives conflict with a local variable or a method, especially when it comes to writing properties. (Downvoting because no technical/practical reason was mentioned.)
    – konsolebox
    Oct 5, 2016 at 7:24
  • 2
    That's your choice. Did you downvote every other person who said they hate it, or did you single me out? You're seven years late to the party. I don't need such a convention to see that I have a name conflict that'll be a problem. Besides, that's trivially easy to sort out: Follow the idiom and distinguish the private member with "this". Your reasoning is...odd. You haven't answered a question here in almost two years and you return to comment on this?
    – duffymo
    Oct 5, 2016 at 11:56
30

It is a matter of personal taste.

Martin Fowler appears to like it. I don't care much for it - it breaks the reading pattern, and the information it conveys is already subtly told by color in your IDE.

I've experimented with using _ as the name of the variable holding the result to be returned by a method. It is very concise, but I've ended up thinking that the name result is better.

So, get your colleagues to agree on what you all like the most and then just do that.

3
  • The variable name consisting of a single underscore may be made invalid in a future version of Java (I've read somewhere) Nov 10, 2015 at 8:23
  • 1
    Will happen in Java 9. Jun 26, 2017 at 21:44
  • 1
    Background info on single underscore symbols can be found in JEP 302. Aug 12, 2017 at 18:06
17

if you want to follow best practice java , use the code convention outlined here http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

I.e., no underscore for private member variable names.

Tho, personally, i m not fussed either way, as long as consistency is applied.

5

Many people (including myself) use _ in front of field names. The reason for this is to easily distinguish them from local variables. However in the ages of IDEs this is not so necessary since syntax highlighting shows this. Using an underscore in front of a class name is just wrong. By convention, class names start with an uppercase letter.

8
  • There are occassions to use a _ in front of a class name, when you are worried that your class name might collide with another class name, and you want to distinguish your class from others. For instance purely internal classes often use this convention to hold things like Record, Data, etc which are common class names, so instead they choose _Record, _Data, etc. Dec 14, 2009 at 8:45
  • Ahh, right, I think I would put the underscore at the end though. But that's a matter of taste I suppose. Dec 14, 2009 at 8:48
  • 3
    _ in front of field names is ugly enough. Using it in Class names is something I wouldn't accept from any developer. There is a Syntax to difference between two classes with the same name, especially inner classes!
    – Hardcoded
    Dec 14, 2009 at 10:13
  • 3
    You don't need to worry about collisions because everything goes by it's fully qualified name internally. Externally if you have a Record class that needs to reference another record class you can just type out 'whatever.other.package.Record' which can be unwieldy but it's not something that comes up to often.
    – Chad Okere
    Dec 14, 2009 at 10:15
  • 3
    My main reason for using a leading _ in field names is to avoid name clashes with local variables, without the need to think up new names or remember to prefix with this.. I also find it less obtrusive than prefixes such as m (Android) or even m_ (I've used that at work, it was the coding style there).
    – starblue
    Dec 13, 2010 at 14:49
3

A lot of people is it for instance variables as it makes them stand out. Personally I hate it mainly because methods should, for the most part, be no more than 10 lines of code (I hardly ever go over 20). If you cannot see that a variable isn't local in under 10 lines then there is something wrong :-)

If you have 100 line methods and you don't declare all your variables at the top of the block that they are used in I can see why you would want to use _ to distinguish them... of course, as with most things, if it hurts then stop doing it!

1

Using _ before a variable helps in one case where if the developer wants to use a standard/key word as a variable name knowingly or unknowingly. It would not give a conflict if that has an additional character.

1

It seems to be a convention that states if an underscore is used as an initial character in the name of method, the method cannot be overridden.

For example, in context of JSP, _jspService() method cannot be overridden.

However, i dont find any document that states above convention.

1
  • 9
    If you want to make sure a method is not overriden you must make it final
    – enrybo
    Feb 6, 2013 at 19:06
0

I agree with the others here... It breaks the standard and doesn't really buy you anything if you just use a naming standard. If you need to know the difference between your class and local variables, try a naming convention like lVariableName where the l is used to indicate that it is local. To me this is unnecessary since the IDE highlighting is sufficient, but might be useful to some.

0
  1. Do not use _ when you use a modern LCD color monitor, with modern IDE like Eclipse, Netbeans, IntelliJ, Android Studio.
  2. Use _ when you need to use a old-style monochrome CRT monitor with only green or white text, or when you have difficulty to identify the color on LCD monitor.
  3. The consensus of your code collaborators overrides the above.
0

I am using _ in front of class name, if the class is package private. So in the IDE I have grouped all package private classes on top of the package and I see immediately, that these classes are not accessable from outside of the package.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.