java naming conventions_java naming conventions

java naming convention

While writing code in Java, it is advisable to follow certain naming conventions. Java naming conventions bring some form of uniformity across your code. It makes your code easy to read for other developers.

When writing code in Java, it is recommended to follow certain naming conventions. Java naming conventions bring some form of uniformity to your code. It makes your code easy to read by other developers.

Although these are not hardcore rules, it is best to follow these practices while writing code.

While these aren't hard and fast rules, it's best to follow these practices when writing code.

1. Java Packages Naming Conventions ( 1. Java Packages Naming Conventions )

  • The package name should be in a small case.

    Package names should be lowercase.
  • In case there are multiple words, separate them by a dot.

    If there are multiple words, separate them with dots.
  • The prefix should be one of the top-level domain names like com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries. (In, Us, Uk)

    The prefix should be one of the top-level domains like com, edu, gov, mil, net, org, or one of the English two-letter codes used to identify the country. (in US, UK)

Example :

example:


package com.journaldev.util;

2. Classes and Interfaces Naming Conventions ( 2. Classes and Interfaces Naming Conventions )

  • Class and Interface names should be nouns.

    Class and interface names should be nouns.
  • They can be in mixed case but the first letter of each internal word should be capitalized. That means the first letter of the class and interface name should be capital too.

    They can be mixed case, but the first letter of each internal word should be capitalized. This means that the first letter of class and interface names should also be capitalized.
  • Avoid acronyms and abbreviations.

    Avoid acronyms and abbreviations.

Example :

example:


public class Vehicle  
{  
//code  
}  

class CarCleaningShop {

}

3. Java Methods Naming Convention ( 3. Java Methods Naming Convention )

  • Methods should be verbs indicative of the functionality of that particular method.

    A method should be a verb indicating the function of that particular method.
  • They can be in mixed case.

    They can be mixed.
  • The first letter should be in lowercase with every subsequent word having the first letter in uppercase.

    首字母应小写,随后的每个单词的首字母均应大写。

Example :

范例:


void slowDown()
{
//code 
} 

void getCustomerAddress() {
}

4. Java变量命名约定 (4. Java Variables Naming Conventions)

  • Variable names should not start with an underscore (_) or dollar sign ($) characters.

    变量名称不应以下划线(_)或美元符号($)字符开头。
  • Start variable names with a lowercase letter with every subsequent word having the first letter in uppercase.

    变量名以小写字母开头,随后的每个单词的首字母均为大写。
  • Avoid using One-character (i,j,k) variable names except in case of temporary throwaway variables.

    避免使用单字符(i,j,k)变量名,除非是临时丢弃的变量。
  • Variable name should be indicative of the variable’s use.

    变量名称应指示变量的用途。

public class Vehicle  
{  
public void slowDown()
{
int speed;
int timeToStop; 
} 
}  

5.常量命名约定 (5. Constants Naming Conventions)

  • A constant name should be in all uppercase.

    常量名称应全部大写。
  • In case there are multiple words, separate them by underscores (“_”).

    如果有多个单词,请用下划线(_)隔开。

Example :

范例:


public class Vehicle { 

static final int MAX_SPEED = 120;

}

结论 (Conclusion )

These are the naming conventions that will make your Java code easy to read. It is not necessary to use these, however, when writing code for production that is going to be read by other people it is best to use Java naming conventions.

这些是使您的Java代码易于阅读的命名约定。 不必使用这些,但是,当编写将要由其他人读取的生产代码时,最好使用Java命名约定。

翻译自: https://www.journaldev.com/42341/java-naming-conventions

java 命名约定

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324128965&siteId=291194637