DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 11 of 59
←PreviousPrevNextNext→

Java Identifiers - Complete Guide

Introduction to Identifiers

Identifiers are names in Java used for classes, variables, methods, packages, and other program elements. They act as labels for referencing code elements.

Clear, descriptive identifiers make code easier to read and maintain. Good naming practices improve collaboration and reduce confusion.

Rules for Java Identifiers

Java enforces strict rules for identifiers. If rules are broken, compilation will fail.

Example
public class IdentifierRules {
    public static void main(String[] args) {
        // Valid identifiers
        int age = 25;
        double _salary = 50000.50;
        String $name = "John";
        int numberOfStudents = 100;
        boolean is_valid = true;

        // Invalid identifiers (uncommenting causes errors)
        // int 2ndValue = 10;
        // double my-salary = 50000;
        // String full name = "John Doe";
        // int class = 5;
        // char #symbol = '#';

        System.out.println("Age: " + age);
        System.out.println("Salary: " + _salary);
        System.out.println("Name: " + $name);
        System.out.println("Students: " + numberOfStudents);
        System.out.println("Valid: " + is_valid);
    }
}
Output
Age: 25
Salary: 50000.5
Name: John
Students: 100
Valid: true

Identifier Naming Conventions

Beyond rules, conventions make code consistent and easier to understand.

Element TypeNaming ConventionExamples
ClassesPascalCaseStudent, BankAccount, StringUtils
InterfacesPascalCaseRunnable, Comparable, List
MethodscamelCasecalculateTotal(), getName(), toString()
VariablescamelCaseage, firstName, totalAmount
ConstantsUPPER_SNAKE_CASEMAX_SIZE, PI, DEFAULT_TIMEOUT
Packageslowercase with dotsjava.util, com.example.myapp
ParameterscamelCaseusername, count, initialValue

Choosing Meaningful Identifiers

Identifiers should clearly express their role and intent.

Example
public class MeaningfulIdentifiers {
    public static void main(String[] args) {
        // Poor choices
        int a = 25;
        double d = 50000.50;
        String s = "John";
        int n = 100;

        // Better choices
        int employeeAge = 25;
        double annualSalary = 50000.50;
        String firstName = "John";
        int maxUsers = 100;

        int retirementAge = 65;
        double monthlySalary = annualSalary / 12;
        String fullName = firstName + " Doe";
        int currentUsers = 75;

        System.out.println("Employee: " + fullName);
        System.out.println("Age: " + employeeAge + "/" + retirementAge);
        System.out.println("Salary: $" + monthlySalary + " monthly");
        System.out.println("Users: " + currentUsers + "/" + maxUsers);
    }

    // Poor method names
    public static void p() {
        System.out.println("Print");
    }

    public static int c(int a, int b) {
        return a + b;
    }

    // Good method names
    public static void printWelcomeMessage() {
        System.out.println("Welcome to our application!");
    }

    public static int calculateSum(int number1, int number2) {
        return number1 + number2;
    }

    public static boolean isValidEmail(String emailAddress) {
        return emailAddress.contains("@");
    }
}
Output
Employee: John Doe
Age: 25/65
Salary: $4166.708333333333 monthly
Users: 75/100

Common Identifier Pitfalls

Avoid these patterns when naming identifiers:

  • ❌ Single-letter names (except loop counters i, j, k)
  • ❌ Unclear abbreviations
  • ❌ Overly long names
  • ❌ Similar names with little distinction (temp1, temp2)
  • ❌ Type prefixes in names (strName, intCount)
  • ❌ Negative boolean names (isNotValid instead of isValid)
  • ❌ Mixing different naming styles in one project

Best Practices for Identifiers

  • ✅ Choose names that show purpose clearly
  • ✅ Prefer clarity over brevity
  • ✅ Use pronounceable words
  • ✅ Make names self-explanatory
  • ✅ Keep naming consistent across the project
  • ✅ Match the name to the abstraction level
  • ✅ Avoid Hungarian notation or encodings
  • ✅ Use nouns for variables and verbs for methods

Java Keywords and Reserved Words

Java keywords and reserved words cannot be used as identifiers since they have special meaning.

Example
public class Keywords {
    public static void main(String[] args) {
        // Examples of reserved words:
        // class, int, public, static, if, else, while, for, return, void, try, catch
        // Also reserved: true, false, null

        System.out.println("Java keywords cannot be used as identifiers!");
    }
}
Output
Java keywords cannot be used as identifiers!
Test your knowledge: Java Identifiers - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 11 of 59
←PreviousPrevNextNext→