Why do we add an _ (underscore) before a variable name? [duplicate]

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;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)