Difference between the static initializer block and regular static initialization

后端 未结 8 1985
广开言路
广开言路 2021-01-05 12:12

As the title says, what exactly is the difference between

public static String myString = \"Hello World!\";

and

public stat         


        
8条回答
  •  走了就别回头了
    2021-01-05 12:38

    A static variable stores a value that is shared between all instances (or the non-instance) of the Class it is defined in.

    A static block is a section of code that gets executed when Class is first loaded.

    "regarding scope, static block is accssible only in the same class"," whereas static variable can be accessed from any class"

提交回复
热议问题