任何人都可以告诉我,类中静态块的用途是什么? [英] Can anyone tell me that what is the use of a static block in a class?
问题描述
public class myclass{
static{
//some statements here
}
//some variables declared here
//some functions defined here
}
推荐答案
这是一个静态
初始值设定项。类似于实例初始化程序(§8.6 ),你可以用它来加载它时初始化你的类
。明确地 NOT 调用;加载类
时会按文本顺序自动执行( static
初始化程序,保证在文本后面出现)稍后在初始化期间执行。)
It's a static
initializer. Analogously to the instance initializer (§8.6), you can use it to initialize your class
when it's loaded. It is NOT "invoked" explicitly; it is executed automatically when the class
is loaded, in textual order (static
initializer that occurs later in the text is guaranteed to be performed later during the initialization).
您可以使用 static
初始化程序:
You can use a static
initializer to:
- 初始化一些
静态
字段 - 执行其他一些 - 时间计算,可能需要
try-catch
块,记录与加载类
相关的事件,确保Java的断言已启用,等等。
- Initialize some
static
fields - Perform some other one-time calculations, perhaps something requiring a
try-catch
block, logging events related to the loading of theclass
, making sure that Java's assertion is enabled, etc.
有一些警告,例如可以重新加载类
,并且通常可以编写它(例如,重构为私有静态
命名方法) ,但使用静态
初始化程序是一个选项。
There are some caveats, e.g. a class
may be reloaded, and usually there are alternatives of writing it (e.g. refactoring into a private static
named method), but using a static
initializer is an option.
- JLS 8.7 Static initializers
- JLS 12.4 Initialization of Classes and Interfaces
这些问题讨论了 static
和实例初始化程序用法的各个方面:
These questions discuss various aspects of static
and instance initializers usage:
- Java中的静态初始化程序
- < a href =https://stackoverflow.com/questions/1866770/how-to-handle-a-static-final-field-init ializer-that-throws-checked-exception>如何处理抛出已检查异常的静态最终字段初始化程序
- 为什么声明的顺序对于静态初始化器很重要?
- 在Java中使用初始化程序与构造函数
- Java静态初始化程序是否安全?
- Static initializer in Java
- How to handle a static final field initializer that throws checked exception
- Why is the order of declarations important for static initializers?
- Use of Initializers vs Constructors in Java
- Are Java static initializers thread safe?
以下是使用断言进行编程的Java语言指南,以确保断言是可行的在运行时编辑:
The following is the prescribed idiom in the Java Language Guide for Programming With Assertions to ensure that assertion is enabled at run-time:
要求断言已启用
程序员某些关键系统可能希望确保在现场禁用断言。如果已禁用其断言,则以下静态初始化习惯用法可防止对类进行初始化:
Requiring that Assertions are Enabled
Programmers of certain critical systems might wish to ensure that assertions are not disabled in the field. The following static initialization idiom prevents a class from being initialized if its assertions have been disabled:
static {
boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side effect!!!
if (!assertsEnabled)
throw new RuntimeException("Asserts must be enabled!!!");
}
把这个静态
-initializer位于班级的顶部。
Put this static
-initializer at the top of your class.
将此代码段放在 static $ c $中c>类的初始化程序,代码将是加载类时执行的第一个代码之一,然后才能创建类的任何实例。代码本身检查是否启用了Java断言,如果不是,则抛出
RuntimeException
。
By putting this snippet in a static
initializer for the class, the code will be one of the first thing executed when the class is loaded, before any instances of the class can be created. The code itself checks if Java assertion is enabled, and throws a RuntimeException
if it's not.
- Wikipedia/Assertion (computing)
- Do you use assertions?
这篇关于任何人都可以告诉我,类中静态块的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!