Java: When is a static initialization block useful?

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

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

public class staticTest {

    sta         


        
13条回答
  •  执念已碎
    2020-11-30 17:06

    In your example, there is no difference; but often the initial value is more complex than is comfortably expressed in a single expression (e.g., it's a List whose contents are best expressed by a for-loop; or it's a Method that might not exist, so exception-handlers are needed), and/or the static fields need to be set in a specific order.

提交回复
热议问题