What is the scope of variables declared inside a static block in java?

前端 未结 4 2010
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 03:36

Are variables declared inside a static block accessible anywhere else? What \"kind\" of member are they(ie., are they static member, too?)

相关标签:
4条回答
  • 2020-12-14 03:53

    Generally programmers don't need to declare any variables inside static blocks, usually this is only for ensuring initialization of static variables for use by all instances of class (depending on scope of static variable).

    Variables declared inside a static block will be local to that block just like methods and constructors variables.

    JDK Docs

    0 讨论(0)
  • Variables declared inside a block are accessible only inside of that block. Static or no.

    Variables declared inside a static method are static. They can only access other static variables or global variables.

    0 讨论(0)
  • 2020-12-14 04:15

    they are like method variables. Only accessible in the block scope.

    0 讨论(0)
  • 2020-12-14 04:16

    No, not visible outside the block. They act like local variables -- think of a static block as an anonymous function that gets called at class initialization. They are not static members.

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