Java / Java 21 Coding Standards Interview Questions
What are the standard naming conventions for methods and variables in Java?
Methods and variables use lowerCamelCase. Method names should be verbs or verb phrases that describe the action performed, such as calculateTotal() or isEligible(), while variable names should be nouns that describe the data they hold, such as orderCount or customerName.
Boolean methods and variables are conventionally prefixed with is, has, or can so the intent reads naturally at the call site, for example hasDiscount rather than discount. Constants declared with static final use UPPER_SNAKE_CASE, such as MAX_RETRY_COUNT.
Single-letter names are acceptable only for very short-lived loop counters like i or j; anything with broader scope, including a variable captured by a lambda or a record component, should carry a descriptive name because it will be read far more often than it is written.
More Related questions...