在Java中使用Initializers和Constructors [英] Use of Initializers vs Constructors in Java

查看:215
本文介绍了在Java中使用Initializers和Constructors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在刷牙我的Java技能,到目前为止,并发现了一些我以前不知道的功能。静态和实例初始化器是两种这样的技术。



我的问题是什么时候使用初始化器,而不是在构造函数中包含代码?我想到了几个明显的可能性:




  • static / instance初始化器可以用来设置final静态/实例变量,而构造函数不能


  • 静态初始化器可用于设置类中任何静态变量的值,在每个构造函数开始处的if(someStaticVar == null)// do stuff代码块




这两种情况都假设设置这些变量所需的代码比简单的var = value更复杂,否则似乎没有任何理由使用初始化器,而不是简单地在声明变量时设置值。



然而,虽然这些并不是微不足道的增益(尤其是设置最终变量的能力),但似乎有一定数量的情况,其中应使用初始化程序。



在构造函数中做了很多事情时,肯定可以使用初始化器,但我真的没有理由这样做。即使一个类的所有构造函数共享大量的代码,使用私有的initialize()函数似乎比使用初始化器更有意义,因为它不会锁定您在编写新的



我错过了什么?是否还有其他一些情况下应该使用初始化程序?

解决方案

静态初始化器作为cletus是很有用的,而且它是一个非常有限的工具,我以相同的方式使用它们。如果你有一个静态变量,当类被加载时被初始化,那么静态初始化是要走的方式,特别是因为它允许你做一个复杂的初始化,并且静态变量仍然是 final 。这是一个大的胜利。



我发现if(someStaticVar == null)//做的东西是混乱和容易出错。如果它被静态初始化并声明为 final ,那么你可以避免 null



但是,当你说:


static / instance initializers可以用来设置final的值
静态/实例变量,而构造函数不能


我假设你说的都是: / p>


  • static初始化器可用于设置final静态变量的值,而构造函数不能

  • instance initializers可用于设置final实例变量的值,而构造函数不能



点,错了第二。例如,您可以这样做:

  class MyClass {
private final int counter;
public MyClass(final int counter){
this.counter = counter;
}
}

此外,当很多代码在构造函数之间共享时,处理这个问题的最好方法之一是链构造函数,提供默认值。这使得很清楚所做的是什么:

  class MyClass {
private final int counter;
public MyClass(){
this(0);
}
public MyClass(final int counter){
this.counter = counter;
}
}


So I've been brushing up on my Java skills as of late and have found a few bits of functionality that I didn't know about previously. Static and Instance Initializers are two such techniques.

My question is when would one use an initializer instead of including the code in a constructor? I've thought of a couple obvious possibilities:

  • static/instance initializers can be used to set the value of "final" static/instance variables whereas a constructor cannot

  • static initializers can be used to set the value of any static variables in a class, which should be more efficient than having an "if (someStaticVar == null) // do stuff" block of code at the start of each constructor

Both of these cases assume that the code required to set these variables is more complex than simply "var = value", as otherwise there wouldn't seem to be any reason to use an initializer instead of simply setting the value when declaring the variable.

However, while these aren't trivial gains (especially the ability to set a final variable), it does seem that there are a rather limited number of situations in which an initializer should be used.

One can certainly use an initializer for a lot of what is done in a constructor, but I don't really see the reason to do so. Even if all constructors for a class share a large amount of code, the use of a private initialize() function seems to make more sense to me than using an initializer because it doesn't lock you into having that code run when writing a new constructor.

Am I missing something? Are there a number of other situations in which an initializer should be used? Or is it really just a rather limited tool to be used in very specific situations?

解决方案

Static initializers are useful as cletus mentioned and I use them in the same manner. If you have a static variable that is to be initialized when the class is loaded, then a static initializer is the way to go, especially as it allows you to do a complex initialization and still have the static variable be final. This is a big win.

I find "if (someStaticVar == null) // do stuff" to be messy and error prone. If it is initialized statically and declared final, then you avoid the possibility of it being null.

However, I'm confused when you say:

static/instance initializers can be used to set the value of "final" static/instance variables whereas a constructor cannot

I assume you are saying both:

  • static initializers can be used to set the value of "final" static variables whereas a constructor cannot
  • instance initializers can be used to set the value of "final" instance variables whereas a constructor cannot

and you are correct on the first point, wrong on the second. You can, for example, do this:

class MyClass {
    private final int counter;
    public MyClass(final int counter) {
        this.counter = counter;
    }
}

Also, when a lot of code is shared between constructors, one of the best ways to handle this is to chain constructors, providing the default values. This makes is pretty clear what is being done:

class MyClass {
    private final int counter;
    public MyClass() {
        this(0);
    }
    public MyClass(final int counter) {
        this.counter = counter;
    }
}

这篇关于在Java中使用Initializers和Constructors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆