关于静态属性的一个有关问题

关于静态属性的一个问题
本帖最后由 misakic52 于 2014-11-21 08:54:42 编辑
public static Map<String,String> map = new HashMap<String,Sring();



和下面这种表达方式有什么区别吗?

public static Map<String,String> map;
static{
map = new HashMap<String,Sring[();
}


这个类我需要用到单例模式,但是有些字段是在构造方法中初始化的,但是在某些情况下当我用到这个map的时候构造方法中的字段还无法初始化,所以我打算作为静态属性来调用(本来这个map就相当于是全局的,基本不会变)。这两种方法声明的静态属性有区别吗
------解决思路----------------------
没有区别,都是加载的时候初始化
区别就是 map = new HashMap<String,Sring>  直接初始化了
map; 先声明
static{  放static块中初始化
------解决思路----------------------
public class Test
{
   public static Test Instance = new Test(2.5);
   private double x;
   public static double y;
  static  {
        y=20.0;
   };
   public Test(double x)
   {
            this.x = x;
   }
  public void Print()
  {
     y=x-y;
    System.out.println(y);
}
}
//执行
 Test.Instance.Print();
  Test  t = new Test(2.5);
   t.Print();

执行下这个例子,就明白初始化块是先于构造函数执行了(手敲的代码,可能有错误)。