HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Basics / Java – Naming Conventions

Java – Naming Conventions

Java naming conventions are sort of guidelines that application programmers are expected to follow to produce a consistent and readable code throughout the application. If teams do not follow these conventions, they may collectively write an application code that is hard to read and difficult to understand.

Java heavily uses Camel Case notations for naming the methods, variables etc. and TitleCase notations for classes and interfaces.

Let’s understand these naming conventions in detail with examples.

1. Package naming convention

Package names must be a group of words starting with all lowercase domain names (e.g. com, org, net, etc). Subsequent parts of the package name may be different according to an organization’s own internal naming conventions.

package com.howtodoinjava.webapp.controller;

package com.company.myapplication.web.controller;

package com.google.search.common;

2. Class naming convention

In Java, class names generally should be nouns, in title-case with the first letter of each separate word capitalized. e.g.

public class ArrayList {}

public class Employee {}

public class Record {}

public class Identity {}

3. Interface naming convention

In Java, interfaces names, generally, should be adjectives. Interfaces should be in titlecase with the first letter of each separate word capitalized. In same cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map.

public interface Serializable {}

public interface Clonable {}

public interface Iterable {}

public interface List {}

4. Method naming convention

Methods always should be verbs. They represent action and the method name should clearly state the action they perform. The method name can be single or 2-3 words as needed to clearly represent the action. Words should be in camel case notation.

public Long getId() {}

public void remove(Object o) {}

public Object update(Object o) {}

public Report getReportById(Long id) {}

public Report getReportByName(String name) {}

5. Variable naming convention

All instance, static and method parameter variable names should be in camel case notation. They should be short and enough to describe their purpose. Temporary variables can be a single character e.g. the counter in the loops.

public Long id;

public EmployeeDao employeeDao;

private Properties properties;

for (int i = 0; i < list.size(); i++) {
	
}

6. Constant naming convention

Java constants should be all UPPERCASE where words are separated by underscore character (“_”). Make sure to use final modifier with constant variables.

public final String SECURITY_TOKEN = "...";

public final int INITIAL_SIZE = 16;

public final Integer MAX_SIZE = Integer.MAX;

7. Generic types naming convention

Generic type parameter names should be uppercase single letters. The letter 'T' for type is typically recommended. In JDK classes, E is used for collection elements, S is used for service loaders, and K and V are used for map keys and values.

public interface Map <K,V> {}

public interface List<E> extends Collection<E> {}

Iterator<E> iterator() {}

8. Enumeration or enum naming convention

Similar to class constants, enumeration names should be all uppercase letters.

enum Direction {NORTH, EAST, SOUTH, WEST}

9. Annotation naming convention

Annotation names follow title case notation. They can be adjectives, verbs, or nouns based on the requirements.

public @interface FunctionalInterface {}

public @interface Deprecated {}

public @interface Documented {}

public @Async Documented {

public @Test Documented {

In this post, we discussed the naming conventions in Java to be followed for consistent writing of code which makes the code more readable and maintainable.

Naming conventions are probably the first best practice to follow while writing clean code in any programming language.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Oliver

    June 18, 2019

    Hi,

    I think No. 8 also contains an error.

    “Similar to class names, enumeration names should be all uppercase letters.”

    Class names use titlecase with nouns, I guess you were thinking “constants” when you wrote this.

    • Lokesh Gupta

      June 18, 2019

      Yeh ! You are right. Thanks for reporting it.

  2. Chris

    February 5, 2019

    I feel that this section:
    In Java, interfaces names, generally, should be verbs.

    Should actually be
    In Java, interfaces names, generally, should be **adjectives**.

    When people suggest things like ‘Runnable’ or ‘Tasklike’ it’s a description of the kind of object that the interface describes. ‘What kind of object is it? It is a {List|Runnable|Cancelable} type of object’

    • Lokesh Gupta

      February 5, 2019

      I think, you are right. Adjectives makes more sense. Even nouns shall be used. Must be thinking something else when I wrote it. Thanks a ton for noticing.

  3. User123

    November 14, 2018

     
    public Object Update(Object o) {}
    

    should be

     
    public Object update(Object o) {}
    
    • Lokesh Gupta

      November 15, 2018

      Thanks for pointing out. I must have been thinking something else. Much appreciated.

Comments are closed on this article!

Search Tutorials

Java Basics

  • Java Introduction
  • Java Installation
  • Java Hello World
  • Java JDK, JRE and JVM
  • Java Classes and Objects
  • Java ClassPath
  • Java Operators
  • Java Data Types
  • Java Primitive Types
  • Java Variables
  • Java Comments
  • Java main() Method
  • Java Command Line Args
  • Java Wrapper Classes
  • Java Types of Statements
  • Java Block Statement
  • Java Pass-by-Value
  • Java System Properties
  • Java Static Import
  • Java hashCode() and equals()
  • Java Immutable Class
  • Java 32-bit vs 64-bit
  • Java java.exe vs javaw.exe
  • Java Generate Bytecode
  • Java Naming Conventions
  • Java Little-Endian vs Big-Endian

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)