Interview Q&A · Java · 7 questions in this topic
Java Coding Standards & Naming Conventions — Java Interview Q&A
Interviewers ask about conventions because readable code is reviewable code — class names, method names, constants and the tools that enforce them. Part of Coding Standards · 294 questions in the Java bank.
What are the Java coding standards for classes?
Answer
Class names are nouns in UpperCamelCase, starting with an uppercase letter, with each inner word capitalised. A source file containing a public class must be named after it, and one top-level public class per file is the rule. Modern teams enforce these conventions automatically with formatters and checkers rather than by review discipline alone.
Key points
- 1Classes: UpperCamelCase nouns (ArrayList, HashMap, EmployeeDetails).
- 2Interfaces/enums/records follow the same class convention; annotations too.
- 3Tooling: google-java-format, Spotless or Checkstyle encode the standard so humans do not have to memorise it.
Example
public class EmployeeDetails { } // noun, UpperCamelCase // file must be EmployeeDetails.java
What are the Java coding standards for interfaces?
Answer
Interface names use the same UpperCamelCase as classes and are usually adjectives describing a capability — Runnable, Serializable, Comparable, Cloneable. Marker interfaces (no methods) like Serializable and functional interfaces (exactly one abstract method) follow the same convention; the difference is in what the name promises, not how it is spelled.
Key points
- 1Adjective-style names signal "can-do": Readable, Iterable, AutoCloseable.
- 2Avoid Hungarian prefixes like "I" (IFoo) — modern Java style omits them.
- 3A @FunctionalInterface documents that the interface is intended for lambdas.
What are the Java coding standards for methods?
Answer
Method names start lowercase in lowerCamelCase and are usually verb phrases — getName(), calculateTotal(), isEmpty(). A boolean-returning method conventionally starts with is/has/can/should. Overloaded methods share one name across compatible parameter lists, and accessors follow the JavaBeans get/set/is pattern that frameworks rely on.
Key points
- 1lowerCamelCase verbs: toString(), addFirst(), computeIfAbsent().
- 2Booleans read as questions: isValid(), hasNext(), canRead().
- 3Getters/setters follow getX/setX/isX so reflection-based frameworks find them.
Example
public String getCarName() { return carName; } public boolean isEmpty() { return size == 0; }
What are the Java coding standards for variables?
Answer
Variable and field names are short, meaningful nouns in lowerCamelCase, starting lowercase. Constants are the exception: static final fields use UPPER_SNAKE_CASE. Single-letter names are acceptable only for tiny scopes (loop indices, a lambda parameter); anything wider deserves a real name.
Example
int employeeCount; String carName; static final int MAX_RETRIES = 3; // constant: UPPER_SNAKE_CASE
What are the Java coding standards for constants?
Answer
Constants — static final fields — are named in UPPER_SNAKE_CASE with words separated by underscores (MAX_VALUE, DEFAULT_TIMEOUT_MS). They are typically public on utility classes, enums or interfaces, declared with an explicit type, and given values that are compile-time constants so the compiler can inline them.
Key points
- 1UPPER_SNAKE_CASE, nouns: MIN_PRIORITY, EMPTY_LIST.
- 2Prefer enums over groups of int constants; prefer static factory constants (List.of()) over mutable statics.
- 3A mutable "constant" (static final List) still has mutable contents — consider List.copyOf or Collections.unmodifiableList.
What are identifiers in Java?
Answer
Identifiers are the names you give to classes, methods, variables, packages and labels. They must begin with a letter, underscore or dollar sign, followed by letters, digits, underscores or dollar signs; Java identifiers are case-sensitive and Unicode-aware. Reserved keywords (class, int, new, ...) can never be identifiers — and since Java 9, even a lone underscore is a keyword and is illegal as an identifier.
Key points
- 1Valid: name, $id, _temp, empName, café (Unicode letters allowed).
- 2Invalid: 2fast (starts with digit), class (keyword), _ alone (keyword since Java 9).
- 3Conventions sit on top: UpperCamelCase types, lowerCamelCase members, UPPER_SNAKE_CASE constants.
What are the naming conventions for packages?
Answer
Package names are all lowercase, dot-separated, and conventionally begin with the reversed domain of the owning organisation — com.example.billing. This guarantees global uniqueness and mirrors the directory layout. The Java platform itself reserves java.* and javax.*; application code should never claim those prefixes.
Example
package com.example.shop.orders; // directory com/example/shop/orders/
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission