Java中的变量命名约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/414001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:20:45  来源:igfitidea点击:

Variable naming conventions in Java?

javanaming-conventionsoop

提问by Click Upvote

In PHP, we (at least the good programmers) always start general variable names with a lower-case letter, but class variables/objects with an upper-case letter to distinguish them. In the same way we start general file names with a lower case letter, but files containing Classes with an upper case letter.

在 PHP 中,我们(至少是优秀的程序员)总是以小写字母开头一般的变量名称,但以大写字母开头的类变量/对象以区分它们。以同样的方式,我们以小写字母开头一般文件名,但包含类的文件以大写字母开头。

E.g:

例如:

<?php
$number=123;
$string="a string";
$colors_array=array('red','blue','red');
$Cat=New Cat();
?>

Are the conventions the same in java, i.e Objects starting with upper-case but the rest with lower case, or does everything start with lower case as I've read in other places?

java中的约定是否相同,即对象以大写开头,其余部分以小写开头,还是如我在其他地方读到的那样,所有内容都以小写开头?

采纳答案by Richard Campbell

Generally, all variables will start with lower case:

通常,所有变量都以小写开头:

int count = 32;
double conversionFactor = 1.5d;

Some people like to put static constants in all case:

有些人喜欢在所有情况下都使用静态常量:

public static final double KILOGRAM_TO_POUND = 2.20462262;

Things get more annoying when you deal with acronyms, and there is no real standard on whether you should use:

当您处理首字母缩略词时,事情会变得更加烦人,并且对于是否应该使用以下内容没有真正的标准:

HTMLHandler myHtmlHandler; 

or

或者

HTMLHandler myHTMLHandler.

Now, either way, note that the class names (Object, String, HTMLHandler) always start with a capital letter, but individual object variables start lowercase.

现在,无论哪种方式,请注意类名(Object、String、HTMLHandler)总是以大写字母开头,但个别对象变量以小写字母开头。

回答by Vincent Ramdhanie

The convention is that class names start with Upper Case letter. Variable names are camelCase. Even if the variable references an object it still starts with lower case.

约定是类名以大写字母开头。变量名是驼峰式的。即使变量引用了一个对象,它仍然以小写开头。

This pageshould help.

这个页面应该有帮助。

回答by EdgarVerona

The conventions really depend on the individual place you're coding for.

这些约定实际上取决于您编码的各个地方。

In general, from what I've seen classes are CamelCased (with upper case first), methods start with lower case, and variables I've seen all over the place (some CamelCased, some camelCase with first letter lower (EDIT: as mentioned above, this is the norm), some even with hungarian notation). It depends on your style and the style that the project you're working on has adopted.

一般来说,从我所看到的类是 CamelCased(大写在前),方法以小写开头,以及我在各处都看到的变量(一些 CamelCased,一些camelCase 首字母小(编辑:如上所述)以上,这是常态),有些甚至使用匈牙利符号)。这取决于您的风格以及您所从事的项目采用的风格。

回答by jamesh

You can find the naming in the Java Code Conventions.

您可以在Java 代码约定中找到命名。

A quick summary:

快速总结:

  • For classes, use UpperCamelCase.
  • For class members and local variables use lowerCamelCase
  • For packages, use reverse URI, e.g. org.acme.project.subsystem
  • For constants, use ALL_CAPS.
  • 对于类,使用UpperCamelCase.
  • 对于类成员和局部变量使用 lowerCamelCase
  • 对于包,使用反向 URI,例如 org.acme.project.subsystem
  • 对于常量,使用ALL_CAPS.

回答by Patrick McElhaney

  • variablesAndMethodsLikeThis
  • ClassesLikeThis
  • CONSTANTS_LIKE_THIS
  • variablesAndMethodsLikeThis
  • 类似这样的课程
  • CONSTANTS_LIKE_THIS

回答by Bill K

Some people (who are not me) like to differentiate Method variables from Instance variables by prefixing Instance variables with "this." This also fixes the problem that occurs when assigning a parameter to an Instance variable of the same name:

有些人(不是我)喜欢通过在实例变量前加上“this”来区分方法变量和实例变量。这也解决了将参数分配给同名实例变量时出现的问题:

public ConstructorMethod(MyVar variable) {
    this.variable=variable;
}

But then some people feel you should always use that pattern--but I'm not crazy about it -- I think it's overkill if you keep your methods and classes small.

但是有些人觉得你应该总是使用这种模式——但我并不为之疯狂——我认为如果你保持你的方法和类很小,那就有点矫枉过正了。

Also, some people use a naming pattern for parameters. This (again) comes in handy when you are assigning from a constructor to an Instance variable:

此外,有些人对参数使用命名模式。当您从构造函数分配给实例变量时,这(再次)派上用场:

public ConstructorMethod(MyVar pVariable) {
    variable=pVariable;
}

Usually the pattern is pVariableor _variable. I occasionally use this because I find it more readable than this, but it has the disadvantage of making your Javadocs less readable.

通常模式是pVariable_variable。我偶尔会使用它,因为我发现它比这更具可读性,但它的缺点是使您的 Javadoc 可读性降低。

In any case, I don't really like the idea of ALWAYS using any of these patterns, they are great to know but if you really need help differentiating them throughout your code, tell Eclipse to show them in different colors.

在任何情况下,我都不太喜欢总是使用这些模式中的任何一个的想法,知道它们很好,但是如果您真的需要帮助在整个代码中区分它们,请告诉 Eclipse 以不同的颜色显示它们。

回答by shx

What do you think about using camelCase for instance methods and PascalCase for static? I think it could be very helpful, because there wouldn't be any issues while verifying which method is static. It's my proposal

您如何看待将驼峰命名法用于实例方法而将 PascalCase 用于静态方法?我认为这可能非常有帮助,因为在验证哪个方法是静态的时不会有任何问题。这是我的提议