Identifiers are an essential part of any programming language, and Java is no exception. In this post, we’ll explore the rules for naming identifiers in Java and discuss some best practices and common mistakes to avoid.
Table of Contents
What are identifiers in Java?
In Java, an identifier is a name used to identify a class, interface, method, or variable. Identifiers can be composed of letters, digits, underscores (_), and dollar signs ($), but they must not begin with a digit.
Example:
int studentId = 5;
In the above code:
int | Data Type |
studentId | An identifier which is a variable |
= | An assignment operator |
5 | A constant value |
; | A line Separator |
Rules to Create Identifiers in Java
An identifier must not start with the digits or number
Example:
int studentId = 5;
studentId
is a valid identifier declaration.
int studentId1 = 1;
studentId1
is also a valid identifier declaration.
int 1studentId = 6;
The 1studentId
declaration here is invalid because the variable name’s started with the number 1.
This rule applies to all the identifiers like classes, interfaces, methods, and so on.
An identifier must not be the keywords of Java
Example:
String return = "Message";
Here, the variable name return
is invalid because the return
is the keyword in Java. Hence, the compiler will not compile this line of code due to an invalid identifier declaration.
An identifier must not be the operators and special symbols other than the $ (dollar) and _ (underscore) symbol
int studentId=123; | Valid Declaration |
String student.name = "Sajan"; | Invalid Declaration |
int student+marks = 80; | Invalid Declaration |
String #address = "Kathmandu"; | Invalid Declaration |
String student@ktm = "Anita"; | Invalid Declaration |
String student-address = "Kapan"; | Invalid Declaration |
String student_name = "Prakash"; | Valid Declaration |
Space is not allowed in identifiers
String student name = "Radha Krishnan";
The variable name student name
is invalid in Java.
Similarly, the method name print name()
is invalid in the code below:
public void print name(){
System.out.println("Radha Krishnan");
}
The correct name of the method should be printName()
. The valid code should be:
public void printName(){
System.out.println("Radha Krishnan");
}
Identifiers should not be repeated in the same scope.
This means if we declare an instance variable called studentId
then we can’t declare another instance variable again with the same variable studentId
irrespective of the data type.
Let’s see an example below:
public class Student {
private int studentId;
private String studentId;
}
The variable declaration in the class Student is invalid because we can’t declare the same variable name more than once in the same scope.
But, we can create the same variable studentId
inside the method as a local variable.
public class Student {
private int studentId;
public void pnintStudentId() {
int studentId = 5;
System.out.println(studentId);
}
}
Here, it is allowed to create the same variable name with different scope but if we try to declare the same variable name inside the method this is also not allowed because of the same scope they have.
This applies to all the identifiers like class, interface, method, and so on.
Hence it is proved that we can’t declare duplicate identifiers in the same scope
Along with these rules, there are some conventions that are widely followed by Java and its communities.
Java Naming Conventions
Java Identifiers should be given a meaningful name
Example:
String aaaa = "Radha Krishnan";
The variable aaaa
has no meaning. Hence it is suggestible to give a proper name so that we can easily identify this variable.
We can write the above statement as :
String name = "Radha Krishnan";
Similarly,
int xyz = 123456;
The variable xyz
has no meaning at all.
Instead, we can write it like this:
int accountNumber = 123456;
There are no restrictions on the length of the identifier but we should follow the camelCase letters
Example:
int studenttemporaryaccountnumber = 123456;
The above variable name is very hard to read. Hence we can write this as:
int studentTemporaryAccountNumber = 123456;
Conclusion
In this post, we learned the identifiers in Java and also learned the rule and conventions of naming the identifiers. Naming identifiers in Java is an important part of writing clear and readable code. By following the rules and best practices outlined in this post, we can ensure that our identifiers are descriptive and meaningful, which will make our code easier to understand and maintain.
Frequently Asked Questions
What is an identifier in Java?
An identifier in Java is a name given to a class, method, variable, or other program elements used to uniquely identify them in the code.
What are the rules for naming identifiers in Java?
Identifiers must start with a letter, underscore, or dollar sign, followed by letters, digits, underscores, or dollar signs. They are case-sensitive and cannot be Java keywords.
Can I use special characters in Java identifiers?
No, Java identifiers cannot contain special characters like @, #, or %.
What is the naming convention for Java identifiers?
By convention, Java identifiers follow camelCase for variables and methods (e.g., myVariable
, calculateTotal()
), and PascalCase for classes and interfaces (e.g., MyClass
, MyInterface
).
Are there any reserved words I cannot use as identifiers?
Yes, Java has reserved keywords like int
, class
, and if
that cannot be used as identifiers in your code.