Skip to content

Java Naming Conventions

Estimated time to read: 1 minute

Overview

  • Names are used to denote classes, objects, attributes and methods
  • A name is a sequence of letters, number and _'s
  • Names may not start with a number
  • Java names are case sensitive

Classes

Class names utilise Camel Case eg ThisIsAClassNameInCamelCase

Other Names

Other names usually start with lower case letters, but are capitalised from there eg setManagerType

Finals / Constants

Finals (Constants) are all upper case as a stylistic convention. Underscores are used to separate individual words. eg THIS_IS_A_FINAL

Reserved Words

Screenshot 2022-07-05 at 12.50.54.png

These are Java's keywords, which signal the compiler to perform some internal operations. These can not be used as identifiers.

Comments

Single line comments

Single line comments start with a //, these can be placed anywhere within the line (unless it is escaped)

public class Car {
 //This is a single line comment
 private int speed; //This is also a comment, but does not affect the code
}

Multi line comments

Multi line comments start with /* and end with a */.

However, if a multi line comment starts with /** instead of /*, the contents of the comment wil be included in the documentation generated by javadoc.

/** Constructor that sets speed
 * We do not want a Car whose speed is
 unknown.
 */
 public Car (int s) {
  speed = s;
 }