Java Naming conventions

17
views
Java naming conventions
(Last Updated On: July 10, 2018)

In this tutorial, you’ll learn about Java Naming conventions used for Java Programming.

Java naming conventions is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method etc. But, it is not forced to follow. So, it is known as convention not rule.

All the classes, interfaces, packages, methods and fields of Java programming language are given according to java naming conventions.

The advantage of naming conventions in java

By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of the Java program is very important. It indicates that less time is spent to figure out what the code does.

Name Convention
class name should start with the uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface name should start with the uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable name should start with lowercase letter e.g. firstName, orderNumber etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
constants name should be in the uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.

Cases in Java Naming Convention:

Using the right letter case is the key to following a naming convention:

Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).

Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).

CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).

Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).

CamelCase in java naming conventions

Java follows camelcase syntax for naming the class, interface, method and variable.

If the name is combined with two words, the second word will start with uppercase letter always e.g. actionPerformed(), firstName, ActionEvent, ActionListener etc.

Examples:

Classes:
class MyClass
{
}
Objects/Variables:
String myName;
MyClass myObject;
Scanner scannerObject = new Scanner(System.in);
Methods:
void myMethod()
{
}
String myName = scannerObject.nextLine();
Constant Variables:
static final char END_OF_FILE = 'e';
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Ask your questions and clarify your/others doubts about Java naming conventions by commenting or posting on our Forum. Java Documentation.

PREVIOUS
JAVA NAMING CONVENTION
NEXT
JAVA PRIMITIVE DATA TYPES