Skip to content
Part IA Michaelmas Term

Java Naming Conventions

Why Conventions Matter

Java has a strong set of naming conventions that are universally followed in professional code. The Tripos explicitly marks adherence to these conventions — a solution that mixes Python’s snake_case with Java code will lose marks even if the logic is correct. Conventions also serve a deeper purpose: they signal the role of an identifier (class, method, variable, constant) without needing to read its declaration.

The Four Case Styles

ConventionApplied ToExample
PascalCase (UpperCamelCase)Classes, interfaces, enumsBankAccount, ArrayList, Comparable
camelCase (lowerCamelCase)Variables, parameters, methodsaccountBalance, findById, firstName
UPPER_SNAKE_CASEConstants (static final fields)MAX_SIZE, DEFAULT_TIMEOUT_SECONDS
lowercasePackage namesjava.util, com.example.myapp

PascalCase for Classes and Interfaces

Every class, interface, and enum name starts with a capital letter and capitalises the first letter of each subsequent word:

public class BankAccount { ... }
public interface Comparable<T> { ... }
public enum DayOfWeek { ... }

Class names should be nouns or noun phrases — they represent things. An interface often names a capability and may be an adjective: Runnable, Serializable, Comparable.

camelCase for Variables and Methods

Variable and method names start with a lowercase letter and capitalise subsequent words:

int studentCount = 0;
String firstName = "Ada";

public double calculateArea() { ... }
public boolean isValid() { ... }

Method names should be verbs or verb phrases — they represent actions. Common prefixes:

  • get / set: accessor and mutator methods (the JavaBeans convention)
  • is / has / can: boolean-returning methods (isEmpty(), hasNext(), canExecute())
  • to: conversion methods (toString(), toCharArray())
  • create / build / of: factory methods (createAccount(), of())

UPPER_SNAKE_CASE for Constants

A static final field whose value is immutable is treated as a constant:

public static final double PI = 3.141592653589793;
public static final int MAX_RETRIES = 3;
public static final String DEFAULT_TITLE = "Untitled";

The static final combination means the value belongs to the class, exists as a single copy, and cannot be reassigned. By convention, the name shouts its constancy with all-caps and underscores.

Meaningful Names

Variable names should describe what the variable stores, not how it is stored:

int d;                     // terrible — meaningless
int elapsedTimeInDays;     // clear but verbose
int daysElapsed;           // good balance of clarity and brevity

Method names should describe what the method does:

public void p(int x) { ... }             // terrible
public void process(int value) { ... }   // better but vague
public void validateInput(int age) { ... } // clear and specific

Single-Letter Names

The only generally accepted single-letter variable names are loop indices:

for (int i = 0; i < n; i++) { ... }
for (int j = 0; j < m; j++) { ... }

The convention is i, then j, then k for nested loops. Avoid using l (lowercase L) because it is easily confused with 1 (digit one).

Java Is Case-Sensitive

count, Count, and COUNT are three distinct identifiers. This compiles (but should never be written):

int count = 1;
int Count = 2;
int COUNT = 3;

Each declaration creates a separate variable. Conflating them in your head because “they’re the same word” is a common source of bugs.

Common Traps for Tripos

Mixing Python and Java Conventions

Python uses snake_case for variables and functions. Java uses camelCase. Writing this in Java is wrong:

int account_balance = 100;      // Python style — incorrect in Java
public void calculate_area() {} // Python style — incorrect in Java

The correct forms:

int accountBalance = 100;
public void calculateArea() {}

Using PascalCase for Variables

int AccountBalance = 100; // looks like a class name — incorrect

The compiler does not complain, but a human reader will mistake AccountBalance for a class. This is the kind of error Tripos “spot the mistake” questions test.

Forgetting That Acronyms Follow the Rules

String XMLParser;    // correct if it's a class
String xmlParser;    // correct if it's a variable
String parseXML();   // correct method name
int HTTPPort;        // preferred over HttpPort (treat HTTP as a unit)

Summary Table

ElementStyleExample
ClassPascalCaseSavingsAccount
InterfacePascalCaseSerializable
MethodcamelCasedepositFunds()
VariablecamelCasecurrentBalance
ConstantUPPER_SNAKE_CASEMAX_BALANCE
Packagelowercasecom.bank.models
EnumPascalCaseTransactionType
Enum constantUPPER_SNAKE_CASETransactionType.WIRE_TRANSFER