Naming Convention

Java Naming Convention

Java naming convention is a set of rule to follow to name the identifiers such as class,variable,package,constant, method, etc.

Java follows CamelCase naming convention for writing names of methods, variables, classes, packages, and constants.
It consists of compound words or phrases such that each word or abbreviation begins with a capital letter or first word with a lowercase letter, rest all with capital.If the name consists of multiple words, first letter will be lowercase, and the first letter of each internal word is capitalized such as actionPerformed(), secondName, onClick, etc.

Classes :

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Use whole words and avoid using acronyms and abbreviations.

class Student{ //code snippet }
class College{ //code snippet }

Interfaces :

Interface names should be capitalized like class names and should be adjective such as Runnable, Serializable, OnClickListener


interface Runnable { //code snippet }
interface Serializable { //code snippet }

Method :

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

class Student
{

void setName() // method
{
//code snippet
}
}

Variable :

Variable names should be short yet meaningful.Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.Variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary “throwaway” variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

Examples :

int i;
char c;

Snippet:

class Employee
{

String name; // variable
double salary;// variable
//code snippet
}

Package :

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.Subsequent components of the package name vary according to an organization’s own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Examples :

com.ksamyatam.production
java.util.*; 
// (* indicates all the classes and interfaces of this package will be accessible but not subpackages.)

Snippet :

com.ksamyatam.production // package
class Employee
{

String name; 
//code snippet
}

Constant :

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores (“_”). (ANSI constants should be avoided, for ease of debugging.).It may contain digits but not as the first letter.There are various constants used in predefined classes like Float, Long, String etc.

Examples :

static final int MIN_WIDTH = 3;
static final int MAX_WIDTH = 999;

Snippet :

class Student
{

static final int MIN_PASS_MARKS = 35; //constant
//code snippet
}
Scroll to Top