Java Naming Conventions


All Java components require names. Names used for classes, variables and methods are called identifiers.

The naming conventions for different Java components are as follows:

Package Naming Convention

Naming conventions for Java packages typically involve using lowercase letters. It's common to use the reverse domain name as a prefix to ensure uniqueness.

Example

com.tutorialspoint.project 

Class Naming Convention

When naming classes in Java, it's essential to start the class name with an uppercase letter. Try to keep the name simple and use nouns to describe the class's purpose.

Example

public class Car {
    private String make;
    private String model;
    
    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }
    
    public String getMake() {
        return make;
    }
    
    public String getModel() {
        return model;
    }
    
    public void drive() {
        System.out.println("The car is driving.");
    }
}

In this example, the class Car starts with an uppercase letter, and the variables make and model start with lowercase letters following the camelCase convention.

Interfaces Naming Convention

Interface names in Java should start with an uppercase letter and typically use camelCase. When naming interfaces in Java, it's common to use a descriptive noun or adjective that represents the behavior or functionality the interface provides.

Example

interface Color;

If you have an interface that defines the color, you could name it as Color.

Method Naming Convention

In method naming convention, we should use verbs that describes the action the method performs. Method names should start with a lowercase letter and typically use camelCase.

public class Rectangle {
    private int length;
    private int width;
    
    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }
    
    public int calculateArea() {
        return length * width;
    }
}

The method calculateArea() follows the naming conventions for methods in Java by using a verb (calculate) to describe the action the method performs.

Constant Naming Convention

When naming constants in Java, it's common practice to use all capital letters with underscores(_) to separate words we can also use the digits but the name should not start with the digits.

Example

public class Example{
    static final double VALUE_OF_PI=3.14159;
    public static void main(String[] args){
        System.out.println("The value of Pi is:"+ VALUE_OF_PI);
    }
}

In the given example, VALUE_OF_PI is the name of constant where we are using all the capital letters with underscores.

Variable Naming Convention

In variable naming convention in Java, we usually write the first word in lowercase and then start each subsequent word with a capital letter. For example, if you have a variable representing a person's age, you could name it personAge.

Example

int personAge;

Quick Summary for Naming Conventions

To meet the coding standards and to make the program more readable, we should follow the below-given rules (naming conventions):

  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.

  • A keyword cannot be used as an identifier.

  • Most importantly, identifiers are case sensitive.

  • Examples of legal identifiers: age, $salary, _value, __1_value.

  • Examples of illegal identifiers: 123abc, -salary.

  • A class name should start from a capital case letter and long names should use camel casing. For example: TaxationDepartment

  • Object name should start from lower case letter and long names should use camel casing. For example: taxationDepartment

Updated on: 03-Jul-2024

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements