File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes variables beginning with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variables beginning with "_"" Watch "variables beginning with "_"" New topic

variables beginning with "_"

Bill Keller

Joined: Aug 03, 2004
Posts: 9
What is the meaning of prepending an underscore in a variable name, such as _myValue ?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

There is no inherent meaning. Variable names are arbitrary.
[ November 08, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bill Keller

Joined: Aug 03, 2004
Posts: 9
When I've seen underscores used, they suggest some pattern or intended meaning by convention. For example, variables using all upper case letters indicate a constant value, such as PI. It seems that is not the case for _size.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Generally, variables beginning with an underscore are system variables, often compiler-generated. Comes from C. Usually, programmers should avoid starting a variable name with the underscore in case there's a system variable with the same name.

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...)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Bill Keller:
When I've seen underscores used, they suggest some pattern or intended meaning by convention.
While it is true that there is no inherent meaning in prepending underscores -- just as there is no inherent meaning to all-caps -- there certainly is personal/group convention.

I've seen it used for the following.
  • Instance variable members
  • Constructor and setter arguments
  • All method arguments (only saw this once)

  • Personally, I dislike the practice for a couple reasons. First, the underscore is a PITA to type and easy to miss when reading code, and it looks ugly as a prefix/suffix. I did say "personally."

    As well, modern IDEs can color-code member and local variables differently if you find you're getting confused. However, in that case I'd say the method is too long and/or not documented well enough.

    For the majority (?) of people, using variable prefixes to denote scope and type has lost favor as OOP has gained in popularity. Nothing screams "exposed implementation details" like a variable named "mi_size".

    In case we ever work together, please don't pick up this style.
    I agree. Here's the link:
    subject: variables beginning with "_"
    It's not a secret anymore!