41

Why do we add an _ (underscore) before a variable name in Java while variable declaration?

I have tried on Google Search, but I actually couldn't find the exact answer for it.

Is it just a type of declaration as the user wants or is there any proper mechanism and reason to do so?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 24
    Well, usually we don't. – Keppil Dec 19 '13 at 09:02
  • I've seen such practice for private variables in flex/action script. But not in Java. – midhunhk Dec 19 '13 at 09:03
  • 1
    @AmanArora - that i also know, I want reason. I am beginner so I am asking, I am not expecting this answer. –  Dec 19 '13 at 09:07
  • To distinguish between Static variables and instance variables,maybe ! – Aman Arora Dec 19 '13 at 09:08
  • As Maroun mentioned its to distinguish between these 2 variables. At my university I actually learned to do it like that in C#, but never saw it in Java... dont know if this is just a microsoft thing or at least more often used there – Ben Dec 19 '13 at 09:12
  • "We" usually don't. It's not the standard Java convention, even if the specification permits it. – chrylis -cautiouslyoptimistic- Dec 19 '13 at 09:12
  • @AmanArora No, actually. Sometimes Java programmers places underscore prefixes to parameter variables to distinguish them from instance (or static, doesn't matter) variables. But it's just a bad habit without any technical justification. – gyorgyabraham Dec 19 '13 at 09:31
  • Kotlin recommends using _ for a private property, "If a class has two properties which are conceptually the same but one is part of a public API and another is an implementation detail, use an underscore as the prefix for the name of the private property." Source: (https://kotlinlang.org/docs/coding-conventions.html#names-for-backing-properties) – A-run Apr 07 '21 at 12:06

4 Answers4

55

Sometimes it is used in order to distinguish class members from local variables:

public class MyClass {
   private int _salary;

   public MyClass(int salary) {
       _salary = salary
   }
} 

However, you should follow Java Naming Conventions that doesn't recommend using that. You can simply name the class member without the leading _ and do:

this.salary = salary;
Wasiqul Islam
  • 303
  • 3
  • 11
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • This is enforced by Resharper. So it's maybe part of IntelliJ IDE. – jnovacho Dec 19 '13 at 09:16
  • 2
    That page is not maintained anymore and may no longer be valid... April 20, 1999! – Yousha Aleayoub Sep 28 '21 at 13:59
  • 1
    @Yousha Aleayoub: Yes, indeed: *"The information on this page is for* ***archive purposes only***. *This page is not being actively maintained. Links within the documentation may not work and the information itself may no longer be valid. The last revision to this document was made on April 20, 1999"* – Peter Mortensen May 17 '22 at 16:51
4

The use of "_" (underscore) must be evaluated carefully.

Use "_" to indicate class attributes is used a lot in C++.

In Java, it is not necessary, because the language has the keyword this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Francesco Irrera
  • 445
  • 4
  • 21
  • 7
    C++ has `this` too, and the underscore has no more or less meaning in C++ than in Java (none, it's just a matter of coding conventions) – Gyro Gearless Dec 19 '13 at 09:32
3

This should be an idiomatic usage, and describe it is a private variable.

Also, this is a way to define a private method in Python to add the "_" as the method prefix like def _privateMethodName(self):.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Liping Huang
  • 4,378
  • 4
  • 29
  • 46
3

An underscore in front usually indicates an instance variable as opposed to a local variable. It's merely a coding style that can be omitted in favor of "speaking" variable names and small classes that don't do too many things.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108