Java has precise rules about identifiers names.
- The name must begin with a letter or the symbol $ or _
- Subsequent characters may also be numbers
- You cannot use the same name as Java reserved word. Please remember Java is case sensitive, so Public and public are different.
a full list of Java Reserved Word
abstract case continue enum float import
native protected strictfp throw void assert
catch default extends for instanceof new
public super throws volatile boolean char
do false goto int null return switch transient
while break byte class const double else final
finally if implements interface long package
private short static synchronized this true try
Note: they are all lowercase && bold could often appear
Example
Which of the following are valid Java identifiers? (Choose all that apply)
A. A$B
B. _helloWorld
C. true
D. java.lang
E. Public
F. 1980_s
Answers
A, B, E.
Option A is valid because you can use the dollar sign in identifiers.
Option B is valid because you can use an underscore in identifiers.
Option C is not a valid identifier because true is a Java reserved word.
Option D is not valid because the dot (.) is not allowed in identifiers.
Option E is valid because Java is case sensitive, so Public is not a reserved word and therefore a valid identifier. Option F is not valid because the first character is not a letter, $, or _.