Variable Naming Conventions in Java

Variable naming conventions in Java

Generally, all variables will start with lower case:

int count = 32;
double conversionFactor = 1.5d;

Some people like to put static constants in all case:

public static final double KILOGRAM_TO_POUND = 2.20462262;

Things get more annoying when you deal with acronyms, and there is no real standard on whether you should use:

HTMLHandler myHtmlHandler; 

or

HTMLHandler myHTMLHandler.

Now, either way, note that the class names (Object, String, HTMLHandler) always start with a capital letter, but individual object variables start lowercase.

What is the proper naming convention for instance variables?

Normally the standard convention in Java is camel case where you start the variable with lower case and using upper case for the first letter of every other word. For example:

int myVariable; 
int mySecondVariable;

But note that these are conventions not rules. So anyone is free to use any convention, but it is recommended to use the standard convention to make the code readable for anyone.

Java constant variable, naming convention

Yes. That is it. It is often used for enum as well.

The only common exception is for logging where you might see

private static final Logger log = Logger.getLogger(getClass().getName());

but I prefer LOG

I often write this as UPPER_CASE, but I also write TitleCase for classes and camelCase for variables and methods.

Use of underscore in variable and method names

See the Java Naming Conventions

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

Good name for a list variable in java

Once upon a time we had Hungarian notation, because IDEs didn't provide intellisense and programmers were too dumb to realise that having a 30,000 line long block of code was unreadable*. Nowadays IDEs are friendly, programmers have started using small, neat classes, we have JavaDoc and things in genral are easier to read.

So, just name your variable for its usage. i.e. names. I'm no fan of pre/post-fixing variables as it often makes them harder to understand.

(*) i.e. Me, there's probably some deeply shameful code out there with my name on it.

Naming Convention for java final variable which is not static

You are talking about a local variable, scoped to your method.
Local variables follow the naming convention for most Java fields, which is camelBack.

Only compile-time constants (static final fields declared at class level) "need" to be capitalized, with words separated by an underscore.

Some doc pages:

  • here, and better,
  • here


Related Topics



Leave a reply



Submit