Best way to name your variables (and not only them) in Java


There are some general rules about naming your variables, fields, parameters and classes in Java.

For class naming camel case with first uppercase letter should be used – MyFirstClass, SomeJavaService etc.

For fields, parameters and variables camel case with first lowercase letter should be used – myVariable, numberForDivision etc.

For constant fields you should use screaming snake case (uppercase words divided by underscores) – MY_CONSTANT, PI etc.

Packages should be named in lowercase, prefer names that consist only from letters.

All variable names must begin with a letter of the alphabet, an underscore ( _ ), or a dollar sign ($). Subsequent characters may be letters, digits, dollar signs or underscore characters. You should prefer using only letters and digits in variable names.

You code get more info about code style from Java Code Convention. Following such convention allows developers to read each others code easier, cause it looks more similar. Also you’d probably like to read Google Java Style Guide, it also provides many rules which help to make your code more readable. Following those rules is not necessary, but is highly recommended.

Reserved keywords

There are some keywords that are reserved for declaring java logical and structural constructs and can’t be used as variable names:


For example:

//Not OK - 'assert' is a keyword
int assert = 1;
int assert1 = 1;


It’s always better to give variables rich, clear names. Shorter is definitely not better.

int x1;
int x2;
int mc;
int maxConfirmationAttempts;
int totalPagesPerVolume;
Scroll to Top