Board Formulas
β˜• JavaπŸ“… Day 3

Identifiers & Keywords in Java

Rules for naming identifiers, list of reserved keywords, and Java naming conventions.

🎯

Learning objectives

  • β†’Write identifiers that follow Java rules
  • β†’Recognise the reserved keywords
  • β†’Apply the standard naming conventions

πŸ’‘ Key points

  • Identifier = name of a class, method, variable, package, or label.
  • Rules: start with letter / _ / $; then any letters or digits; no keywords; case-sensitive.
  • Java has 50+ reserved words β€” they cannot be used as identifiers.
  • Convention: classes PascalCase, methods/vars camelCase, constants SCREAMING_SNAKE_CASE, packages lowercase.dot.form.

πŸ’» Code examples(2)

#1Valid vs invalid identifiers
java
// valid
int age;
String userName;
double _total;
long $money;
int itemCount1;

// invalid β€” will not compile
// int 1item;       // starts with digit
// int user-name;   // hyphen not allowed
// int class;       // reserved keyword
Digits allowed only after the first character. Symbols other than _ and $ are illegal. Reserved words like class/if/for cannot be identifiers.
#2Naming conventions
java
public class BankAccount {           // Class β†’ PascalCase
    static final double MIN_BALANCE = 500.0;   // constant

    private String accountHolder;    // instance var β†’ camelCase

    public double calculateInterest() {        // method β†’ camelCase
        return accountHolder != null ? 0.05 : 0.0;
    }
}

🎯 Practice

Q1. Is `2ndPlayer` a valid Java identifier? Why?+

No. Identifiers cannot start with a digit.

Q2. Which of these are keywords: class, Class, static, String?+

class and static are reserved keywords. Class and String are classes (identifiers), not keywords.

Q3. Rename `int TOTAL_score` to follow Java conventions.+

int totalScore β€” variables use camelCase; SCREAMING_SNAKE_CASE is reserved for constants (final).

πŸ“ Notes

Reserved keywords (partial list)

abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while.

true, false, null are literals β€” also cannot be used as identifiers.

Quick tips

  • Choose meaningful names β€” emailAddress beats ea.
  • Avoid single letters except for loop counters (i, j, k).
  • No length limit, but keep names readable.