Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What are the naming conventions in Java?

Tarun Telang

Overview

Naming conventions in Java are a set of rules that apply to Java programs, and are defined in The Java Language Specification. They include recommendations for naming packages, classes, methods, and variables.

This shot discusses some basic naming conventions in Java.

PascalCasing

PascalCasing is a naming convention where each word in a compound word is capitalized. "JavaNamingConventions" is an example of a PascalCased word in Java.

camelCasing

In camelCasing, the first word in a compound word is not capitalized. "javaNamingConventions" is an example of a camelCased word in Java.

snake_casing

In snake_casing, underscores separate the words in a compound word. "java_naming_conventions" is an example of a snake_cased word in Java.

Naming conventions in Java

Construct

Convention

Description

Examples

Class

PascalCase

Classes should be named using PascalCasing, with the first letter of each word capitalized. A class name should be a noun or a word representative of an object.

MyClass, Employee

Interface

PascalCase

Interfaces should be named using PascalCasing, with the first letter of each word capitalized. An interface name should be an adjective.

Serializable, Runnable

Method

camelCase

Method should be named using camelCasing. Method names should always start with a lower case letter.

main(), print(), println() etc.

Variable

camelCase

Variables should be named using camelCasing. Variable names should always start with a lower case letter.

num1, name, address

Package

snake_case (all lower case)

Packages should be named using snake_casing. Package names should always start with a lower case letter.

java, lang, util

Constant

snake_case

(ALL UPPER CASE)

Constant variables should be named using snake_casing. Contant variable names should have all upper case letters.

MAX_VALUE, MIN_VALUE etc.

Code example

Below is a code example demonstrating the usage of Java naming conventions.

package io.educative.example;
public class Employee{
  public static final int MAX_AGE=100; //Constant variable naming convention
  private String empName;  //Variable naming convention
  private int age;

  public Employee(String name, int age){  //Constructor naming convention
    this.empName=name;
    this.age=age;
  }

  public void setName(String name){  //Method naming convention
    this.empName = name;
  } 

  public int getAge(){  //Method naming convention
    return age;
  }

  public static void main(String args){  //Main method naming convention
    Employee emp1 = new Employee("John Doe", 30);
    System.out.println("Employee Name: "+emp1.empName);  //Prints Employee Name
    System.out.println("Employee Age: "+emp1.getAge());  //Prints Employee Age
  }
}
Java code demonstrating naming conventions

Explanation

In the above code example, we follow different naming conventions for different parts of the code. We use PascalCasing for class and interface names, camelCasing for method and variable names, and snake_casing for constant variable names. We also use lower case letters for package names.

Following the Java naming conventions makes our code more readable and consistent. It is not mandatory to follow these conventions, but it is generally a good idea to do so.

RELATED TAGS

java
RELATED COURSES

View all Courses

Keep Exploring