Java Identifiers
Rules and best practices for naming in Java
🏷️ What are Java Identifiers?
Java identifiers are names given to variables, methods, classes, and other program elements. They must follow specific rules and conventions to make your code readable and error-free.
public class StudentRecord { // Class identifier
String studentName = "John"; // Variable identifier
int studentAge = 20; // Variable identifier
public void displayInfo() { // Method identifier
System.out.println("Name: " + studentName);
System.out.println("Age: " + studentAge);
}
}
Output (when displayInfo() is called):
Name: John
Age: 20
Identifier Rules
Starting Characters
Must start with letter, _, or $
int age; // ✅ Valid
int _count; // ✅ Valid
int $price; // ✅ Valid
Cannot Start With
Cannot start with numbers
int 2count; // ❌ Invalid
int number1; // ✅ Valid
int num2ber; // ✅ Valid
Case Sensitive
Upper and lower case matter
int age = 25;
int Age = 30; // Different variable
int AGE = 35; // Also different
Reserved Words
Cannot use Java keywords
int class; // ❌ Invalid
int public; // ❌ Invalid
int myClass; // ✅ Valid
🔹 Valid Identifier Examples
Here are examples of valid Java identifiers:
public class ValidIdentifiers {
public static void main(String[] args) {
// Valid variable names
int age = 25;
String firstName = "Alice";
double _salary = 50000.0;
boolean $isActive = true;
int number1 = 10;
String user_name = "alice123";
double PI_VALUE = 3.14159;
// Valid but not recommended
int $ = 100; // Valid but confusing
String _ = "underscore"; // Valid but unclear
int a1b2c3 = 42; // Valid but not descriptive
// Print some values
System.out.println("Age: " + age);
System.out.println("Name: " + firstName);
System.out.println("Salary: $" + _salary);
System.out.println("Active: " + $isActive);
System.out.println("Number: " + number1);
System.out.println("Username: " + user_name);
System.out.println("PI: " + PI_VALUE);
}
}
Output:
Age: 25 Name: Alice Salary: $50000.0 Active: true Number: 10 Username: alice123 PI: 3.14159
🔹 Invalid Identifier Examples
These identifiers will cause compilation errors:
public class InvalidIdentifiers {
public static void main(String[] args) {
// ❌ These will cause compilation errors:
// int 2number = 10; // Cannot start with digit
// String first-name; // Cannot contain hyphen
// double my salary; // Cannot contain space
// boolean class = true; // Cannot use Java keyword
// int public = 5; // Cannot use Java keyword
// String @email; // Cannot start with @
// int #count; // Cannot contain #
// double 123abc; // Cannot start with number
// ✅ Correct versions:
int number2 = 10; // Start with letter
String firstName; // Use camelCase instead of hyphen
double mySalary; // No spaces, use camelCase
boolean isClass = true; // Add prefix to avoid keyword
int publicCount = 5; // Combine with other word
String email; // Remove special character
int count; // Remove special character
double abc123; // Start with letter
System.out.println("All identifiers are now valid!");
}
}
Output:
All identifiers are now valid!
🔹 Naming Conventions
Follow these conventions for better code readability:
📋 Java Naming Conventions:
- Variables & Methods: camelCase (firstName, calculateTotal)
- Classes: PascalCase (StudentRecord, BankAccount)
- Constants: UPPER_CASE (MAX_SIZE, PI_VALUE)
- Packages: lowercase (com.example.myapp)
// Good naming convention examples
public class BankAccount { // Class: PascalCase
private String accountNumber; // Variable: camelCase
private double currentBalance; // Variable: camelCase
private static final double MIN_BALANCE = 100.0; // Constant: UPPER_CASE
public void depositMoney(double amount) { // Method: camelCase
currentBalance += amount;
System.out.println("Deposited: $" + amount);
System.out.println("New balance: $" + currentBalance);
}
public static void main(String[] args) {
BankAccount myAccount = new BankAccount();
myAccount.currentBalance = 500.0;
myAccount.accountNumber = "ACC123456";
System.out.println("Account: " + myAccount.accountNumber);
System.out.println("Balance: $" + myAccount.currentBalance);
System.out.println("Minimum required: $" + MIN_BALANCE);
myAccount.depositMoney(250.0);
}
}
Output:
Account: ACC123456 Balance: $500.0 Minimum required: $100.0 Deposited: $250.0 New balance: $750.0
🔹 Reserved Keywords
These words cannot be used as identifiers because they have special meaning in Java: