Java-编程规范

Java编程规范

  • import中不能有通配符。
  • 重载方法应该放在一起连续出现。
  • 即使代码块中没有内容,或者只有一行代码,也必须使用大括号。
  • 2空格缩进。
  • 列宽是80或100个字符。
  • 不能使用类似C语言的数组变量声明。
  • switch语句中,必须包含default语句。
  • 修饰符的顺序应按照Java语言规范推荐的顺序。
  • 常量命名应使用CONSTANT_CASE格式(译注:所有字母大写,单词使用下划线分隔)。注意,所有常量都必须是static final成员,但并不是所有的static final成员都是常量
    并不是static final 修饰的变量都是编译期常量
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    public class Test {
    public static void main(String[] args){
    // 情况一 基本数据类型
    //System.out.println(Outer.Inner.n);
    // 输出结果 : 10

    //情况二
    //Outer outer = Outer.Inner.outer;
    // 输出结果 : 静态内部类

    //情况三
    System.out.println(Outer.Inner.str);
    // 输出结果: 静态内部类 abc

    //情况四
    System.out.println(Outer.Inner.str01);
    // 输出结果: abc
    }
    }
    class Outer{
    static class Inner{
    public static final String str = new String("abc");
    public static final String str01 = "abc";
    public static final Outer outer = new Outer();
    public static final int n = 10;
    static {
    System.out.println("静态内部类");
    }
    }
    }
    String 是一个比较特殊的类(str01: static final 修饰的变量不可变 , “abc”这个字符串也不可变)