Keywords and Identifiers

Reserved words and naming conventions

Interview Relevant: Common interview questions about reserved keywords
6 min read

What are Keywords?

Keywords are reserved words that have special meaning in Java. They cannot be used as variable names, method names, or class names. Java has 50+ keywords that control the flow and structure of programs.

Key Fact: Keywords are case-sensitive. For example, class is a keyword, but Class is not.

Categories of Keywords

  • Access Modifiers: public, private, protected, default
  • Data Type Keywords: int, float, double, char, boolean, byte, short, long
  • Class/Object Related: class, interface, extends, implements, new, this, super
  • Method Related: void, return, static, final, abstract
  • Control Flow: if, else, switch, case, for, while, do, break, continue
  • Exception Handling: try, catch, finally, throw, throws
  • Package Related: package, import

What are Identifiers?

An identifier is a name given to variables, methods, classes, and other elements. Unlike keywords, identifiers are user-defined names that follow specific naming conventions.

Naming Rules for Identifiers

  • Must start with a letter, underscore (_), or dollar sign ($)
  • Cannot start with a number
  • Can contain letters, numbers, underscores, and dollar signs
  • Cannot be a reserved keyword
  • Are case-sensitive: age and Age are different
  • No length limit (but keep them reasonable)

Naming Conventions

While Java allows flexibility in naming, conventions make code more readable:

  • Variables & Methods: camelCase (firstName, calculateTotal)
  • Classes & Interfaces: PascalCase (Student, PaymentProcessor)
  • Constants: UPPER_SNAKE_CASE (MAX_VALUE, PI)
  • Packages: lowercase with dots (com.example.util)

Code Examples

Examples of valid and invalid identifiers in Java

java
1// Valid identifiers
2int age = 25;
3String firstName = "John";
4float PI = 3.14159f;
5boolean isStudent = true;
6
7// Invalid identifiers (uncomment to see errors)
8// int 1student = 10;        // cannot start with number
9// int class = 5;            // class is a keyword
10// String first-name = "John"; // hyphens not allowed

Java naming conventions for different types of identifiers

java
1// Proper naming conventions
2public class Student {          // PascalCase for classes
3    private String firstName;   // camelCase for variables
4    public static final double PI = 3.14159; // CONSTANT_CASE
5    
6    public void calculateGrade() {  // camelCase for methods
7        // method implementation
8    }
9}

Use Cases

  • Writing clean, readable code that follows Java conventions
  • Understanding why certain names cause compilation errors
  • Following team coding standards and best practices
  • Debugging variable naming issues

Common Mistakes to Avoid

  • Using keywords as identifiers (causes compile error)
  • Starting variable names with numbers
  • Using hyphens or special characters in names
  • Inconsistent naming conventions in the same project
  • Using single-letter variable names except for loops