How to Choose the Perfect Names in Java

What Should I Name My Identifier?

Cloudage
Names that you choose for a variable in Java is called an identifier. You can't choose just any name, and this article will explain how to choose good names for identifiers in Java.

In theory an identifier can be any length. It has to start with a letter, an underscore or a dollar sign. It's proper naming convention to start with a small letter, and then for every new word in the identifier, capitalize the first letter. An example is "numberOfHouses". You don't have to follow this rule, but most Java programmers do. Whatever you choose to stick with, you must do it consistently. If you start one variable with a small letter, do the same for all the other ones as well.

Except for the first letter, you can use any characters besides those used as operators in Java (ex. +, -, /, *). However, unless you have a very good reason to use signs, it's usually best to go with letters, digits and the underscore.

It's important to take notice of the fact that in Java, numberOfHouses is not the same as NumberOfHouses. Java is case sensitive, so pay special attention to this when naming an identifier.

There are also some words that cannot be used, mainly Java keywords. Examples are private, while, if, do, int, long and char. When you put together a name yourself, you are almost guaranteed to be safe, so this is very easy to avoid. Examples could be num1, num2 etc.

You should always choose names for variables that clearly says what kind of data they hold. In "numberOfHouses", there is no doubt that this variable will hold an unknown number of houses. If I named the variable "house", there could be confusion as to what data this variable holds. It could be concerning owners, colors, decorations, building dates, construction and so on. If you know what the data is by reading the variable, it's a good name.

Java source code is in Unicode, and this means that you can use any character defined within the Unicode character set. You can name your variables with Russian, Norwegian, or German characters. The reason for this is because even though you write the source code in ASCII characters, the source code is converted to Unicode characters internally, before it's compiled.

That being said, it's very important to choose good names for your variables. The Java naming conventions are very straight forward, and by following the few rules above you'll be in the safe zone. If you are confused about choosing appropriate names, it might help to picture someone else reading your source code. Would they understand the different identifiers, and what they stand for? Have you been consistent in using the same capitalization all around? Have you avoided the Java no-no keywords? If yes, then you're most likely on the right track!

Published by Cloudage

I am a student studying and tutoring in math, chemistry and physics.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.