Comments, Keywords and Naming Conventions in Java
The three comment forms, the reserved words you cannot use as names, and the naming conventions every Java codebase follows.
-
Java Basics
- Introduction to Java
- Setting Up Java and Writing Your First Program
- Variables, Data Types and Literals in Java
- Type Casting and Type Conversion in Java
- Operators and Expressions in Java
- Input and Output in Java
- Comments, Keywords and Naming Conventions in Java
- Control Flow in Java: if, else and switch
- Loops in Java: for, while and do-while
- Methods
- Arrays and Strings
-
OOP
- Classes and Objects in Java
- Constructors in Java
- The this Keyword in Java
- The static Keyword in Java
- Encapsulation in Java
- Access Modifiers in Java
- Inheritance in Java
- Method Overriding and super in Java
- Polymorphism in Java
- Abstraction, Abstract Classes and Interfaces in Java
- Composition, Aggregation and Association in Java
- The Object Lifecycle in Java
- Core Java
- Exception Handling
-
Collections
- The Java Collections Framework
- List in Java: ArrayList, LinkedList, Vector and Stack
- Set in Java: HashSet, LinkedHashSet and TreeSet
- Map in Java: HashMap, LinkedHashMap and TreeMap
- How HashMap Works Internally in Java
- Queue and Deque in Java: ArrayDeque and PriorityQueue
- Iterators in Java
- Comparable and Comparator in Java
- Collections Utilities and Choosing the Right Collection
- Generics
- Java 8+
- Stream API
- Date and Time
- File and I/O
-
Multithreading
- Threads in Java: Processes, Runnable and Thread
- Thread Lifecycle in Java
- Synchronization in Java: synchronized and volatile
- Locks and Atomic Classes in Java
- Race Conditions and Deadlocks in Java
- The Executor Framework and Thread Pools in Java
- Future and CompletableFuture in Java
- Concurrent Collections in Java
- The Java Memory Model
- JVM and Memory
- Advanced Java
- Networking
- JDBC
- Testing
Comments
// A single line comment, used for short notes.
/*
A block comment.
Useful for a longer explanation.
*/
/**
* A documentation comment. Tools read these and generate API documentation.
*
* @param amount the value to convert, never negative
* @return the converted value
*/
public double convert(double amount) { ... }Only the third form is structured. It sits directly above a class, method or field and supports tags such as @param, @return, @throws and @deprecated.
Comments should explain why, not what. A comment restating the code becomes wrong the moment the code changes, and a stale comment is worse than none at all.
Keywords
Keywords are reserved and cannot be used as identifiers. Grouped by purpose they are easier to remember than as an alphabetical list.
| Purpose | Keywords |
|---|---|
| Primitive types | byte short int long float double char boolean void |
| Declarations | class interface enum extends implements package import |
| Modifiers | public protected private static final abstract synchronized native transient volatile strictfp default |
| Control flow | if else switch case for do while break continue return |
| Exceptions | try catch finally throw throws assert |
| Object related | new this super instanceof |
| Reserved but unused | goto const |
true, false and null are literals rather than keywords, but they are equally reserved and cannot name anything.
Contextual keywords
Newer features avoided reserving more words by making them special only in certain positions. var, record, sealed, permits and yield are still legal as variable names, although using them that way is a poor idea.
Identifier rules
- Start with a letter, an underscore or a currency symbol; digits are allowed after the first character.
- No spaces and no operators.
- Case sensitive:
totalandTotalare different identifiers. - A single underscore on its own is not a valid identifier in modern Java.
Naming conventions
| Element | Convention | Example |
|---|---|---|
| Class, interface, enum, record | UpperCamelCase, a noun | InvoiceService |
| Method | lowerCamelCase, a verb phrase | calculateTotal |
| Variable and field | lowerCamelCase | orderCount |
| Constant | UPPER_SNAKE_CASE | MAX_RETRIES |
| Package | all lower case, dotted | com.example.billing |
| Type parameter | a single capital letter | T, K, V, E |
| Boolean accessor | reads as a question | isActive, hasStock |
These are conventions rather than compiler rules, but they are followed almost universally, and breaking them makes code look wrong to every Java reader.
Common mistakes
- Starting a class name in lower case, which makes it look like a variable.
- Abbreviating past the point of clarity:
calcTotAmtsaves nothing worth having. - Commenting out dead code and leaving it. Delete it; version control remembers.
- Writing a documentation comment that repeats the method name and adds no information.
Best practices
- Name things after the domain, not after their type.
customerbeatscustomerObject. - Document public API with
/** ... */and keep the rest of the code self explanatory. - Reserve comments for intent, constraints and the reason an unusual choice was made.
Practice
- Which of these are legal identifiers:
2ndValue,_count,total value,Var,class? - Rename
public class studentdataand its methodpublic void GetName()to follow convention. - Write a documentation comment for a method that withdraws money and throws when the balance is insufficient.
Conclusion
Conventions are how Java code stays readable across teams. Follow them without argument, and spend your judgement on naming things accurately instead.