在java中的静态块内声明的变量范围是什么? [英] What is the scope of variables declared inside a static block in java?

查看:176
本文介绍了在java中的静态块内声明的变量范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在静态块内声明的变量是否可以在其他地方访问?
他们是什么样的成员(即,他们也是静态成员吗​​?)

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

推荐答案

程序员不需要在静态块中声明任何变量,通常这只是为了确保静态变量的初始化以供所有类实例使用(取决于静态变量的范围)。

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.

< a href =https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html\"rel =noreferrer> JDK Docs

这篇关于在java中的静态块内声明的变量范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆