日期:2014-05-20  浏览次数:20755 次

静态变量实例化问题呀
class Single {
  private Single() {}

  private static Single instance = new Single();

/* private static Single instance;
  instance = new Single()
*/
我用注释里的来实例化,编译为什么会出错,说是需要标识符

------解决方案--------------------
Java code
public class Single {
    private Single() {}

    private static Single instance = new Single();

    private static Single instance(){
        instance = new Single();
        return instance;
    }

}

------解决方案--------------------
Java code

class Single {
  private Single() {}

  // private static Single instance = new Single(); //假如注释掉

  private static Single instance; //这句话没有问题
  // instance = new Single(); //这句活有问题,因为这条语句不能直接放在这里,必须放在某个方法里面或者放在初始化块里面。