Java: When is a static initialization block useful?

前端 未结 13 1621
时光说笑
时光说笑 2020-11-30 16:49

What\'s the difference between initialization within a static block:

public class staticTest {

    sta         


        
13条回答
  •  渐次进展
    2020-11-30 17:04

    Technically, you could get away without it. Some prefer multiline initialisation code to go into a static method. I'm quite happy using a static initialiser for relatively simple multistatement initialisation.

    Of course, I'd almost always make my statics final and point to an unmodifiable object.

提交回复
热议问题