• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Java | Naming Conventions
  1. Identifier Naming Rules
  2. Identifier Naming Conventions (Java code conventions)
  3. JavaBeans Property Naming Conventions
  4. Java Source File Rules

  1. Identifier Naming Rules
    An identifier is a name that refers to a type, a variable, or a method.
    • The first letter of the identifier's name must be:
      • a letter ([A-Z|a-z] or a Unicode character),
      • a currency symbol ($),
      • or an underscore (_).

      Example variable names:
      int c;
      int $;
      char \u0061; // \u0061 : a
      int _; // Not allowed with Java 11
    • The first letter of the identifier can be followed by:
      • a combination of letters,
      • digits,
      • the currency symbol ($),
      • and the underscore character (_).

      Example variable names:
      int cb3$_;
      int $c1$_;
      int _a2$_;
      char c\u0061;
    • Identifier names cannot contain spaces or symbols like "+" or "©".

    • Identifier names cannot use Java keywords such as "public", "static", "void", etc.

    • Identifier names are case-sensitive (intVar is different from INTVAR).
  2. Identifier Naming Conventions (Java code conventions)
    • Class names:
      The first letter of each word in the class name should be uppercase.
      Class names should be nouns (example: GenericServlet).

    • Interface names:
      The first letter of each word in the interface name should be uppercase.
      Interface names should be adjectives (example: Serializable).

    • Variable names:
      The first letter should be lowercase, and then each following word should start with an uppercase letter (example: servletName).

    • Method names:
      The first letter should be lowercase, and then each following word should start with an uppercase letter (example: getServletName).
      Method names should follow the verb-noun format.

    • Constant names:
      Constant names should be written in uppercase.
      Words in the constant name should be separated by underscores (example: SC_NOT_FOUND).
      Constants should be declared using the modifiers public, final, and static.
  3. JavaBeans Property Naming Conventions
    • Getter (accessor) names should start with "get" (example: getValue).
      Boolean properties can have getter names that start with "is" (example: isValid).

    • Setter (mutator) names should start with "set" (example: setValue).

    • Getter and setter declarations must use the public keyword.

    • A getter must not take any parameters and the return type must match the type of the property it represents: PROPERTY_TYPE getPROPERTY_NAME().

    • A setter must return void and must have a parameter that matches the type of the property it represents: void setPROPERTY_NAME(PROPERTY_TYPE PROPERTY_NAME).

    Example:  /general/src/ca/mtitek/javabeans/MyJavaBean.java
    package com.mtitek.javabeans;
    
    public class MyJavaBean {
        Double value;
    
        public Double getValue() {
            return value;
        }
    
        public void setValue(Double value) {
            this.value = value;
        }
    }
  4. Java Source File Rules
    • Java source files must have the .java extension.

    • A source file can have only one public class declaration.
      In other words, a source file can have multiple class declarations but only one class can be public.

    • If the source file contains one public class, then the file name must match that class name.
      If the source file contains multiple class definitions, then the file name must match the name of the public class.
      If the source file contains no public class, the file name can be different from the names of the classes inside it.

    • The file name must be unique within a package, as well as the names of the classes defined in each file.

    • If the source file is part of a package, then the code must explicitly use the package keyword to specify that package.
      The package declaration must be the first line of code (excluding comments and blank lines).

    • The source file code may use the import keyword to specify dependencies on external classes.
      import statements should come right after the package statement (excluding comments and blank lines).
© 2025  mtitek