What is an initialization block?

后端 未结 10 2080
青春惊慌失措
青春惊慌失措 2020-11-22 03:42

We can put code in a constructor or a method or an initialization block. What is the use of initialization block? Is it necessary that every java program must have it?

10条回答
  •  無奈伤痛
    2020-11-22 04:12

    Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces. It is not at all necessary to include them in your classes.

    They are typically used to initialize reference variables. This page gives a good explanation

提交回复
热议问题