Java Naming Rules


Java is an object-oriented programming language. It is class based. Every Java class can have class components such as methods and variables. Java code is compiled into byte code for interpretation by the JVM. Java comes with naming rules that are required to be followed by all component names. In other words, all class, interface, method and variable names are required to follow Java naming rules. In addition to the naming rules, Java components cannot use Java keywords as component names.

Java keywords

Java has 50 reserved words that are used to define the basic components of the language. These are called Java keywords. Java keywords are used for the basic built-in functionalities of the languages, such as loops or function return types. Since using these words would confuse the compiler, keywords cannot be used as component names. The Java keywords are, in alphabetical order

abstract default if private this
assert do implements protected throw
boolean double import public throws
break else instanceof return transient
byte enum int short try
case extends interface static void
catch final long strictfp volatile
char finally native super while
class float new switch const
continue for package synchronized goto

const and goto are not used anymore with the Java programming language but they are still reserved words and cannot be used as an identifier.

Also, true, false and null are not keywords but they are literals. A literal is simply the value assigned to a Java primitive. true and false are literals assigned to boolean variables. null is for assigning to objects that are not initialized. These three literals cannot be used as Java identifiers either.

Java naming rules

Every class and class component is referred to by its name in a Java program. Like every programming language, Java language defines a set of rules to follow while naming classes and class components. The class and component names that follow these rules are referred to as legal identifiers and the ones that do not are referred to as illegal identifiers. A Java class with illegal identifiers cannot compile.

The rules for legal identifiers can be summarized as follows.

1-Variable names cannot begin with a number. The following is an illegal declaration and will cause a compilation error.

int 1a= 14;

2- Variable names can have only digits, unicode characters, currency symbol ($), and underscore sign (_). Unicode characters include upper and lower case characters from most of the written languages of the world. Characters outside this system, like @, >, *, are considered special characters and not allowed in a variable name. Though currency symbol and underscore sign are allowed in a legal identifier, it is considered a bad practice to have these characters in a Java component name.

The following are illegal declarations.

int a@b = 15;

String p* = “beautiful”;

The following declarations are legal but considered bad practice.

int _a = 15;

String $p = “beautiful”;

3- Except the first character, a legal identifier can be of any length and can have any number of unicode characters, digits, currency symbols and underscores with any order.

4- A variable cannot have whitespace in it. The following declaration is illegal and it will cause a compilation error.

int a b = 3;

5- Java keywords cannot be used as legal identifiers.

What is CamelCase Notation?

CamelCase Notation is a naming convention that is followed while naming classes, methods and variables in Java, as well as other programming languages. If the name of a component is a compound word or phrase, the words inside the name are joined without spaces and all the words other than the first one start with an uppercase letter for better readability. The first word starts with an uppercase letter in Upper CamelCase Notation, which is followed by class names. Other component names follow Lower CamelCase notation where the first word starts with a lowercase letter.

CamelCase Notation is a convention that is used to follow variables names easily. Not following it does not cause a compilation error but makes the code harder to read.

Java Naming Conventions

Now that we know about legal identifiers and CamelCase Notation, we can talk about Java Naming Conventions. Java Naming Conventions are a set of rules that are recommended to be followed as a best practice while naming Java language components. Not following these rules does not cause a compiler error but code that follows these rules is more clean and easier to follow.

Java Naming Convention rules can be summarized as follows.

1- Class and Interface Names: Class and Interface names follow Upper CamelCase notation. For example, MyClass.

2- Method Names: Method names follow Lower CamelCase notation. It is generally considered a good practice to name methods in a meaningful do-it form, related to the functionality of the method. For example, getBalance.

3- Variable Names: Variable names follow Lower CamelCase notation. For example, myVariable.

4- Constants: Constants in Java language are typically final static variables and their names are all in uppercase. If the constant name contains more than one word, the words are separated by underscores. For example, MY_CONSTANT.

%d bloggers like this: