Naming Conventions in java

Naming Conventions java

Naming conventions in the Java programming language help Java programmers create and maintain readable, properly structured program source code in a clear, easily readable format. Following proper naming rules and regulations in the Java language not only helps make your program source code easier to read and update. A programmer’s naming conventions should also comply with modern rules and regulations and industry standards, allowing both new and established software developers to collaborate and communicate with the global developer community, making remote development easier. Here, we’ll explain Java naming conventions in detail. Universal elements like Java classes, methods, parameter variables, constants, packages, and more are explained in detail.

Naming Conventions in java

So, let’s take a closer look at the naming conventions, rules, and regulations in Java programming.

Class and interface names.

Class and Interface Naming Rules – Apply the concept of PascalCase, also known as UpperCamelCase, to the names of classes and interfaces defined in user-created Java programs.

The PascalCase method in Java naming rules here means that the programmer capitalizes the first character of each word and does not add spaces between them. Java-created class names should be in noun format and represent the entity or concept in the defined class to which they are directly related.

Interface names in Java programs typically indicate a role or behavior, or may be preceded by a descriptive adjective, such as “Editable” or “Upgaradeable.”

Examples of class and interface names.

User Account

Login Authentication

Upgradable (Interface)

Editable (Interface)

Java Method Naming Rules.

Method Naming Rules – Use the CamelCase concept for method name rules in Java programs. CamelCase rules in Java programs mean that the first word of a declared Java method must be declared in lowercase, and the first character of every subsequent word must be capitalized. You cannot add spaces to the naming rules. Declared class method names must be in a simple, verb-like format, indicating the action the method performs in the current program.

Example Java Method Names.

addTotal()

displayEmployeeName()

bankPayment()

searchValue()

Java Variable Naming Rules.

Variable Naming Rules – Use the CamelCase concept for variable name declarations in Java programs. Similar to the Java method name rules, program-declared variables should also be named using the CamelCase concept. When declaring variables in Java programs, their names should be descriptive and detailed, and the variable’s data type should be properly indicated. Always use simple and meaningful name formats for declaring variables in Java programs, using word text that indicates the universal variable’s purpose in the current program. Avoid using single-letter variables in programs, except for reserved keywords, loops, or temporary scenarios, such as p, q variables for indexes.

Example of Java Variable Naming Rules.

empName

discountPayment

fileSeries

isLogin

Java Constant Names Rules.

Constant naming rules – Use uppercase text with an underscore symbol to separate words when declaring variables in Java programs.

Java constants are static final variables, which must be provided with a name in uppercase character format, and variable words must be separated by an underscore symbol (_). Remember, Java program constant names generally represent a fixed value that, once declared, remains unchanged throughout the program.

Example of Java Constant Names.

HIGEST_VALUE

TEMP

DEFAULT_RESULT

MIN_INTEGER

Java Package Name Rules.

Package naming rules – Use lowercase characters when creating package names in Java programs, and avoid underscores. User-declared package names must be in lowercase character format. Package Naming Rules: To avoid naming problems, especially for large-volume projects, use reverse domain names. If the user’s domain is sample.com, the package can start with com.sample.

Example of Java Package Name Rules.

com.sample.testapp

org.vcanhelpsu.project

com.systemapp.node

com.host.app

Java Parameter Name Rules.

Parameter naming rules – Use the camelCase concept for method parameters in Java programs. Like variables and methods, parameters declared in Java programs must be indicated in the camelCase name format. Program parameters declared here must be provided with names representing their values.

Example of Java Parameter Name Rules.

employeeName

empId

salary

textList

Enum Constants Name Rules.

Enum Constants Naming Rule – Use the uppercase character format with an underscore symbol to separate enum data type words in Java programs. Remember, enum constants declared in Java programs should also be represented separately by an uppercase character format with an underscore symbol, just like constant variables.

Example of Enum Constants Naming Rules.

SUNDAY

RISK_CHOICE

AVAILABLE

ADVANTAGE

WEEKDAY

Java Abstract Class and Interface Naming Rules.

Abstract Class and Interface Naming Rule – Java programs should follow the PascalCase concept for naming abstract classes, and Java program interface names should indicate any default behavior or capability.

  • Java Abstract Class – The concept of providing an abstract class name in a Java program is similar to that of regular class names (PascalCase). Most often, abstract class names are also nouns, indicating a basic idea, concept, or category in a program.
  • Java interfaces – Interfaces in Java programs typically use adjectives or suffixes like “able” or “ible” to indicate their capabilities.

Example Java abstract class and interface names.

Triangle (abstract class)

ArcDrawer (abstract class)

Understandable (interface)

Customizable (interface)

Java local variable naming rules.

Local variable naming rules – Follow the CamelCase concept for declaring local variables in Java programs. Declare them with short identifier names but meaningful variable names. Local variables in Java programs are typically declared inside methods or blocks and must be given descriptive names. Typically, programs use short variable names like p, q, r for looping. However, for other program purposes, more descriptive local variable names should be used.

Example of Java local variable naming rules.

age

indexing

piValue

garbage

name

Accessor and mutator methods (getters and setters) in Java.

Accessor and mutator method naming rules – Use the get prefix for getters and set for setters in Java programs.

  • Java getters – A getter is a Java method that retrieves the value of a private field in a program.
  • Java setters – A setter is also a method that sets or modifies the value of a private field in Java.

Example of accessor and mutator methods in Java.

getEmployeeName() – Getter for the private employeeName field

setEmpAge(int age) – Setter for the private empage field

Java Boolean Variable Naming Rules.

Boolean Variable Naming Rules – Use prefix words like is, has, can, or should for Boolean variable and method name declarations in Java programs.

Remember that Boolean data types represent true/false values. Therefore, using these prefixes helps indicate the nature of the variable to the program.

Example of Java Boolean variable naming rules.

isValid

hasAllow

canRun

shouldPreview

Detail Explanation of Java Naming Conventions

Element of namingNaming ConventionNaming Convention Example
ClassesFollow PascalCase (UpperCamelCase)s rules for namingEmployee, BankDetail
InterfacesApply PascalCase (UpperCamelCase) format for naming conventionValueable, Uderstandable
MethodsUse camelCase (lowerCamelCase) format for naming conventionaddTotal(), getEmployeeName()
VariablesApply camelCase (lowerCamelCase) format for naming conventionempName, allAmount
ConstantsApply UPPERCASE with underscores format for naming conventionHIGEST_VALUE, TEMP
PackagesApply lowercase, no underscores format for naming conventioncom.sample.testapp
Enum ConstantsUse UPPERCASE with underscores format for naming conventionSUNDAY, WEEKDAY
Local VariablesUse camelCase (lowerCamelCase) format for naming conventionsystemValue, garbage
Getters/SettersUse get and set prefix word format for naming conventiongetEmployeeName(), setEmployeeAge()
Boolean VariablesUse is, has, can, should prefixes word format for naming conventionisAllow, hasEnable

Java Abbreviation and Acronym Naming Rules.

Abbreviation and Acronym Rules – Avoid abbreviations in Java programs unless they are well-recognized or very commonly used. If programmers use abbreviations in Java programs, try to keep them consistent throughout their program code. Do not try to change them. When using abbreviations in a Java program, first ensure they are capitalized in the order they appear in URL or HTML.

Example of Java abbreviation and acronym rules.

htmlText (not htmlText)

empId (not empid)

hostAddress (not hostaddress)

Leave a Reply