Difference between the static initializer block and regular static initialization

后端 未结 8 1987
广开言路
广开言路 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

    with static blocks you can change the order of the initialisation different to the order of declaration.

    0 讨论(0)
  • 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"

    0 讨论(0)
提交回复
热议问题