服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

静态和实例初始化块的使用说明

    对象通常是有状态的,比如一个员工有姓名、年龄等字段。我们一般使用构造器对对象进行状态设置,本文将介绍使用静态和实例初始化块来对对象进行状态初始化的工作。

    首先我们来看一个简单的例子,代码如下:
public class smallsquares
{

    private static final int limit = 10;

    private static final int[] square = new int[limit];

    public smallsquares()
    {
        for (int i = 0; i < limit; i++)
        {
            square[i] = i * i;
        }
    }

    public static int getsquare(int i)
    {
        if (i < 0 || i > limit)
        {
            system.out.println("error:the i should between 0 to 10");
            return -1;
        }
        return square[i];
    }

    public static void main(string[] args)
    {
        new smallsquares();
        system.out.println("3 squared is " + getsquare(3));
    }
}
这个小程序用来计算0-10的平方,这个程序是在构造器中对数组进行初始化的。其实这个构造器的唯一目的就是来初始化这个数组的。可是我们仔细考虑一下,这个类的功能比较简单,内部的状态也不复杂。但是如果要依靠构造器来初始化状态的话,显然不合适。我们每次必须要首先调用smallsquares()才可以让程序工作,不然会出现nullpointerexception。考虑用static初始化块来完成这个功能,使用它的话我们就可以不用构造器了,由于是static的,因此只会被执行一次。下面给出优化过的代码,注意绿色部分。
public class smallsquares
{

    private static final int limit = 10;

    private static final int[] square = new int[limit];

    static
    {
        for (int i = 0; i < limit; i++)
        {
            square[i] = i * i;
        }
    }


    public static int getsquare(int i)
    {
        if (i < 0 || i > limit)
        {
            system.out.println("error:the i should between 0 to 10");
            return -1;
        }
        return square[i];
    }

    public static void main(string[] args)
    {
        new smallsquares();
        system.out.println("3 squared is " + getsquare(3));
    }
}

    如果去掉static关键字,那么这就是实例初始化块了。实例初始化块会在构造器之前被系统调用。看下面的简单例子。
public class constructorexample
{
    private final string username;

    private final static int[] square = new int[10];

    public constructorexample()
    { 
        this("anonymous");
    }

    public constructorexample(string username)
    {
        this.username = username;
        for (int i = 0; i < 10; i++)
        {
            square[i] = i * i;
        }
    }

    public void printsquare(int i)
    {
        if (i < 0 || i > 10)
        {
            system.out.println("error:the i should between 0 to 10");
            return;
        }
        system.out.println("hello " + username);
        system.out.println(i + " squared is " + square[i]);
    }

    public static void main(string[] args)
    {
        new constructorexample().printsquare(3);
        new constructorexample("ed").printsquare(5);
    }
}

    上面的代码如果用实例初始化块,怎在效率以及合理性上都显得更好。也可以避免构造器串联在一起。
优化后的代码如下:
public class constructorexample2
{

    private final string username;

    private static final int[] square = new int[10];
    {
        for (int i = 0; i < 10; i++)
        {
            square[i] = i * i;
        }
    }

    public constructorexample2()
    {
        username = "anonymous";
    }

    public constructorexample2(string username)
    {
        this.username = username;
    }

    public void printsquare(int i)
    {
        if (i < 0 || i > 10)
        {
            system.out.println("error:the i should between 0 to 10");
            return;
        }
        system.out.println("hello " + username);
        system.out.println(i + " squared is " + square[i]);
    }

    public static void main(string[] args)
    {
        new constructorexample2().printsquare(3);
        new constructorexample2("ed").printsquare(5);
    }
}

扫描关注微信公众号