Identifiers in Java
Identifiers in Java are symbolic names used for identification. They can be a class name, variable name, method name, package name, constant name, and more. However, In Java, There are some reserved words that can not be used as an identifier.
For every identifier there are some conventions that should be used before declaring them. Let's understand it with a simple Java program:
From the above example, we have the following Java identifiers:
let's understand the rules for Java identifier:
Rules for Identifiers in Java
There are some rules and conventions for declaring the identifiers in Java. If the identifiers are not properly declared, we may get a compile-time error. Following are some rules and conventions for declaring identifiers:
Java Reserved Keywords
Java reserved keywords are predefined words, which are reserved for any functionality or meaning. We can not use these keywords as our identifier names, such as class name or method name. These keywords are used by the syntax of Java for some functionality. If we use a reserved word as our variable name, it will throw an error.
In Java, every reserved word has a unique meaning and functionality.
Consider the below syntax:
in the above statement, double is a reserved word while marks is a valid identifier.
Below is the list of reserved keywords in Java:
Although the const and goto are not part of the Java language; But, they are also considered keywords.
Example of Valid and Invalid Identifiers
Following are some examples of valid identifiers in Java:
Below are some examples of invalid identifiers:
We should follow some naming convention while declaring an identifier. However, these conventions are not forced to follow by the Java programming language. That's why it is called conventions, not rules. But it is good to follow them. These are some industry standards and recommended by Java communities such as Oracle and Netscape.
If we do not follow these conventions, it may generate confusion or erroneous code.
See more about Java Naming Conventions.