Best Practices for Naming Java Identifiers: Classes, Methods, Constants & Packages - Quiz

Total: 4 questions

1. 

What are the naming conventions for classes and interfaces in Java?

Class and interface names use UpperCamelCase (PascalCase): the name starts with a capital letter, every following word also starts with a capital letter, and there are no underscores: Cat, Address, HashMap. Class names are usually nouns. Interface names are often adjectives that describe a capability, such as Runnable, Comparable or Iterable, but nouns are common too: List, Map.

2. 

What are the naming conventions for methods in Java?

Method names use lowerCamelCase: the first word starts with a lowercase letter and every following word with a capital letter. A method performs an action, so its name is usually a verb or a verb-noun pair: getSalary(), calculateTotal(), setEmployeeName(). Methods that return boolean usually start with is, has or can: isEmpty(), hasNext().

3. 

What are the naming conventions for variables in Java?

Variables (fields, local variables and parameters) use lowerCamelCase: zipCode, textSize, startDate. A name should be short but meaningful; single-letter names such as i or j are acceptable only for loop counters. Variable names should not start with _ or $, even though the compiler allows it.

4. 

What are the naming conventions for constants in Java?

A constant is a static final field whose value never changes. Its name is written in UPPER_SNAKE_CASE: uppercase letters with words separated by underscores, for example MAX_WEIGHT or DEFAULT_TIMEOUT:

public static final int MAX_WEIGHT = 100;
Page 1 of 1