Naming Conventions
Part of the Fundamentals section of Coddy's Java journey — lesson 11 of 73.
In Java, it's important to follow naming conventions to keep your code readable and maintainable. Here are some key rules:
- Variable Names:
- Use
camelCase: Start with a lowercase letter, then capitalize the first letter of each subsequent word (e.g.,firstName,studentCount). - Choose descriptive names that indicate the variable's purpose (e.g.,
userAgeinstead ofua). - Avoid single-letter names except for simple loop counters.
- Use
- Constant Names:
- Use
UPPER_SNAKE_CASE: Write in uppercase letters, with words separated by underscores (e.g.,MAX_VALUE,PI_VALUE). - Use for values that don't change throughout the program.
- Use
- General Rules:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must start with a letter, an underscore
_, or a dollar sign$.
- General Rules:
- Names are case-sensitive (
myVariableis different frommyvariable). - Avoid using Java's reserved keywords (like
int,class,public, etc.).
- Names are case-sensitive (
Following these conventions helps make your code more understandable, especially when working in teams or revisiting your code later.
Cheat sheet
Java naming conventions help keep code readable and maintainable:
Variable Names:
- Use
camelCase: Start with lowercase, capitalize first letter of subsequent words - Choose descriptive names:
firstName,studentCount,userAge - Avoid single-letter names except for loop counters
Constant Names:
- Use
UPPER_SNAKE_CASE:MAX_VALUE,PI_VALUE - For values that don't change throughout the program
General Rules:
- Names can contain letters, digits, underscores, and dollar signs
- Must start with a letter, underscore
_, or dollar sign$ - Names are case-sensitive
- Avoid Java reserved keywords
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else