Java一行变量声明? [英] Java one line variable declaration?
问题描述
在我的Java类中,我声明这样的变量
In my Java class i am declaring variable like this
BigDecimal sumFeeBilled = new BigDecimal(0), sumPaid = new BigDecimal(0);
或者我们必须在多行中声明这样
Or we have to declare like this in multiple line
BigDecimal sumFeeBilled = new BigDecimal(0);
BigDecimal sumPaid = new BigDecimal(0);
我们应该遵循哪一个?
推荐答案
这只是品味和偏好的问题。但是,如果你没有设定指导方针,它将成为大多数开发团队中无休止的辩论/争论的温床,与Vim vs Emacs或IntelliJ vs Eclipse一起。
This is simply a matter of taste and preference. However if you don't set guidelines it will become a hotbed of endless debate/arguments in most development teams, alongside Vim vs Emacs or IntelliJ vs Eclipse.
我想做什么建议为您的团队设置编码标准,最简单的方法是引用已存在的标准,例如Sun(现在的Oracle)Java指南,在这种情况下建议每行使用一个声明。
What I would recommend is setting coding standards for your team, and the simplest way to do this is to reference already-existing ones such as the Sun (now Oracle) Java Guidelines which in this case suggest using one declaration per line.
这里有Sun的权威指南关于声明的说法[1]:
Here what Sun's definitive guide says about declarations[1]:
建议每行一个声明,因为它鼓励评论。换句话说,
One declaration per line is recommended since it encourages commenting. In other words,
int level; // indentation level
int size; // size of table
优先于
int level, size;
不要在同一行上放置不同的类型。示例:
Do not put different types on the same line. Example:
int foo, fooarray[]; //WRONG!
[1] http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#2991
这篇关于Java一行变量声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!