Java Naming Conventions

Java naming convention is a rule to follow as you decide what to name your identifiers such as class, interface, package, variable, constant, method etc.

They must be followed while developing software in java for good maintenance and readability of code.
Java uses CamelCase as a practice for writing names.

Camel case in Java Programming: 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.

1. Classes and Interfaces:
Class names should have first letter uppercase and first letter of each internal word capitalized.
Interfaces name should also have first letter uppercase and first letter of each internal word capitalized.

This naming method is popularly called as UpperCamelCase.
Ex:- Name, FirstName

Examples:
Interface  Bicycle
Class MountainBike implements Bicyle

Interface Sport
Class Football implements Sport

2. Methods:
Methods name should have first letter lowercase and with the first letter of each internal word capitalized.

This naming method is popularly called as lowerCamelCase.
Ex:- name, firstName

Examples:
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);

3. Variables/Objects:
Variable  name should have first letter lowercase and with the first letter of each internal word capitalized.

This naming method is popularly called as lowerCamelCase.
Ex:- name, firstName

Examples:
// variables for MountainBike class
    int speed = 0;
    int gear = 1;

4. Constant Variables:
In Java constant variables are declared using “static final” modifiers. And such variables must contain all UPPERCASE characters and multiple words must be seperated using ‘_’.

Ex:- NAME, FIRST_NAME

Examples:
static final int MIN_WIDTH = 4;

// Some  Constant variables used in predefined Float class
public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;

5. Packages:
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, like com, edu, gov, mil, net, org.
Subsequent components of the package name vary according to an organization’s own internal naming conventions.

Ex:- name

Examples:

com.sun.eng
com.apple.quicktime.v2
// java.lang packet in JDK
java.lang

Happy Learning !

Comments

Popular posts from this blog

Explain public static void main in Java

Reserve or Key Words in Java

Strings 01 - Write a program to reverse a string in java using for loop