C

Core Java tutorial for beginners

Clean • Professional

Core Java Keywords & Identifiers – Rules, Examples & Best Practices

3 minute

Java Keywords & Identifiers

This guide explains what keywords and identifiers are in Java, their rules, best practices, and examples. It’s beginner-friendly, interview-ready, and optimized for Google ranking.


What Are Keywords in Java?

  • Keywords are reserved words in Java that have a special meaning in the language.
  • You cannot use keywords as names for variables, classes, methods, or identifiers.
  • Java has 52 keywords (depending on version) and 5 reserved literals like true, false, null.

List of Java Keywords (Java 17)

KeywordKeywordKeywordKeyword
abstractassertbooleanbreak
bytecasecatchchar
classconst*continuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgoto*ifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhileyieldrecord
sealedpermitsnon-sealedvar
moduleopenrequiresexports

⚠️ const and goto are reserved but not used in Java.


What Are Identifiers in Java?

  • Identifiers are names you give to classes, methods, variables, and other elements in Java.
  • They help uniquely identify these program elements in your code.

Rules for Identifiers

  1. Must start with a letter, $, or _.
  2. Can contain letters, digits, $, or _.
  3. Cannot be a keyword.
  4. Java is case-sensitive (ageAge).
  5. No spaces or special characters allowed.

Valid Identifiers

int age;
String studentName;
double _salary;
int $count;

Invalid Identifiers

int 2age;       // starts with a digit
String student-name; // contains hyphen
int class;      // keyword

Naming Conventions (Best Practices)

ElementConventionExample
ClassPascalCaseStudentDetails
MethodcamelCasecalculateSalary()
VariablecamelCasetotalSalary
ConstantUPPERCASEMAX_AGE
Packagelowercasecom.example.project

Following conventions improves readability, collaboration, and maintainability.


Example Program Using Keywords & Identifiers

public class Student {

    // Instance Variables (Identifiers)
    String studentName;
    int age;
    final double PI = 3.14159; // Constant

    // Method
    public void displayInfo() {
        System.out.println("Name: " + studentName);
        System.out.println("Age: " + age);
        System.out.println("PI Value: " + PI);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.studentName = "John";
        s1.age = 25;
        s1.displayInfo();
    }
}

Output:

Name: John
Age: 25
PI Value: 3.14159

🏆 Top 5 Java Interview Questions - Java Keywords & Identifiers

1. What is the difference between a keyword and an identifier?

Answer:

FeatureKeywordIdentifier
DefinitionReserved word in Java with a special meaningName given to variables, methods, classes, etc.
UsageCannot be used as namesCan be used to name user-defined elements
Exampleclass, int, if, formyVar, calculateTotal, Student
ModifiableNoYes

2. How many keywords are there in Java 17?

Answer:

  • Java 17 has 51 reserved keywords, including sealed, non-sealed, and record.
  • Example keywords: abstract, assert, boolean, break, case, class, continue, enum, final, for, if, import, instanceof, new, switch, while, record, sealed.

3. Can an identifier start with a number? Why or why not?

Answer:

  • No, an identifier cannot start with a digit.
  • Reason: Java’s syntax rules require identifiers to start with a letter (A–Z, a–z), underscore (_) or dollar sign ($).
  • Example of invalid identifiers: 1var, 2num
  • Example of valid identifiers: var1, _count, $value

4. Explain the difference between final, static, and regular variables.

Answer:

TypeDescriptionExample
Regular VariableStandard variable tied to an object (instance) or method (local)int age = 25;
Static VariableShared by all objects of a class; belongs to class, not instancestatic int count = 0;
Final VariableConstant; value cannot be changed after initializationfinal double PI = 3.14159;

Notes: A variable can be both static and final (constant shared by all instances).


5. Is class a valid identifier? Why or why not?

Answer:

  • No, class is not a valid identifier.
  • Reason: It is a reserved keyword in Java used to define classes, and keywords cannot be used as names for variables, methods, or classes.

Article 0 of 0