`
OuYangGod
  • 浏览: 53026 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

JAVA初始化

 
阅读更多
  在JAVA中变量在使用前,必须初始化,否则在编译时会报错。
public void sample()
{
    int i;
    System.out.println(i);// Error -- i not initialized
}

  但是当变量是类的属性时,情况就有些不一样了。
public class Sample
{
    int i;
    Object o;
    
    public static void main(String[] args)
    {
        new Sample().print();
    }
    
    public void print()
    {
        System.out.println("int = " + i);// int = 0
        System.out.println("Object = " + o);// Object = null
    }
}

  从运行结果来看,类的属性总是被自动初始化的。

指定初始化
1、在定义的时候初始化
public class Sample
{
    int i = 1;
    Object o = new String("hello world");
    
    public static void main(String[] args)
    {
        new Sample().print();
    }
    
    public void print()
    {
        System.out.println("int = " + i);// int = 1
        System.out.println("Object = " + o);// Object = hello world
    }
}

2、在构造方法中初始化
public class Sample
{
    int i;
    Object o;
    
    public Sample(int i, Object o)
    {
        this.i = i;
        this.o = o;
    }
    
    public static void main(String[] args)
    {
        new Sample(1, "helloworld").print();
    }
    
    public void print()
    {
        System.out.println("int = " + i);// int = 1
        System.out.println("Object = " + o);// Object = hello world
    }
}

  很明显,第二种方法更为灵活,可以在运行时动态指定各个属性的初始值。
  值得注意的是,无论哪种方法,属性都会先被自动初始化为默认值。比如第二种方法,可以通过下面的代码看出属性先是被自动初始化为默认值;对于第一种方法,后面会给说具体的例子说明。
public class Sample
{
    int i;
    Object o;
    
    public Sample(int i, Object o)
    {
        System.out.println("int = " + this.i);// int = 0
        System.out.println("Object = " + this.o);// Object = null
        this.i = i;
        this.o = o;
    }
    
    public static void main(String[] args)
    {
        new Sample(1, "helloworld").print();
    }
    
    public void print()
    {
        System.out.println("int = " + i);// int = 1
        System.out.println("Object = " + o);// Object = hello world
    }
}

初始化的顺序
1、在类中属性初始化的顺序是跟定义它们的顺序一样的。虽然在类里面属性和方法的定义可能会相互交错,但是在任何方法被调用之前,所有的属性都会先被初始化。
public class Sample
{
    Item item1 = new Item(1);
    
    public Sample()
    {
        System.out.println("Constructor sample");
    }
    
    Item item2 = new Item(2);
    
    public void f()
    {
        System.out.println("In f()");
    }
    
    Item item3 = new Item(3);
    
    public static void main(String[] args)
    {
        new Sample().f();
    }
}

class Item
{
    Item(int i)
    {
        System.out.println("Item " + i);
    }
}

  结果如下
Item 1
Item 2
Item 3
Constructor sample
In f()

2、static属性的初始化
  static属性总是在需要的时候才被初始化,而且仅被初始化一次,因为static属性(或static方法)是属于类的,是无状态的。在加载某个类的时候,static属性会在所有non-static属性被初始化之前被初始化。
  以下例子可以看出,static的item3对象在item1和item2之前被初始化:
public class Sample
{
    Item item1 = new Item(1);
    
    public Sample()
    {
        System.out.println("Constructor sample");
    }
    
    Item item2 = new Item(2);
    
    public void f()
    {
        System.out.println("In f()");
    }
    
    static Item item3 = new Item(3);
    
    public static void main(String[] args)
    {
        new Sample().f();
    }
}

class Item
{
    Item(int i)
    {
        System.out.println("Item " + i);
    }
}

  结果如下
Item 3
Item 1
Item 2
Constructor sample
In f()

3、类加载过种总结
  • 当第一次显示通过new创建某个对象、或者直接访问某个类的static方法(或static属性)时,JAVA虚拟机会在classpath中找到这个类的class文件;
  • 当class文件被加载后,类中所有的static属性将被初始化。(注意,static属性仅会被初始化一次;
  • 当使用new创建该类的一个新对象时,JAVA虚拟机会在堆中分配足够的空间来存放这个对象;
  • 新分配的空间中,所有的原始类型将被置0(int类型为0,boolean类型为false等等),引用类型将被置成null;
  • 接着进行显示初始化,即属性在定义时指定的初始化;
  • 最后类的构造方法执行。
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics