CodeBlanks Logo Icon CodeBlanks

Naming variables

Learn camelCase and valid naming rules.

Naming variables

Java variable names use camelCase: the first word starts lowercase, the following words start uppercase. Example: studentGrade, totalPrice.

Naming rules:

  • Can contain letters, digits, $, _
  • Cannot start with a digit
  • Cannot contain spaces
  • Reserved keywords (class, int) are not allowed

Descriptive names make code more readable.

Practice Define a camelCase variable

Write a Main class with a main method. Assign 85 to int studentGrade. Print it with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        int studentGrade = 85;
        System.out.println(studentGrade);
    }
}

Expected output

85

Naming tips

A good variable name clearly says what it holds. Avoid vague names like x or data. Names like userCount, totalAmount are descriptive.

English names make the code easier to share internationally. Non-ASCII letters are technically allowed but not recommended.

Abbreviations should be consistent: either always cnt or always count. Mixing hurts readability.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is a valid variable name?

  • 1number
  • number-grade
  • class
  • studentGrade Correct answer

Explanation: studentGrade is camelCase and not a keyword.

2 Match the naming conventions

Term Meaning
camelCase Variable
PascalCase Class
ALL_CAPS Constant
snake_case Not Java

3 Complete the camelCase variable

Question
int ____ = 90;
Answer
int studentGrade = 90;

Explanation: studentGrade is a camelCase variable name.

All lessons in Beginner