Java没有名称静态方法 [英] Java no name static method

查看:93
本文介绍了Java没有名称静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么?

public class ABC {
   public ABC() {
         System.out.println("world");
   }
   static {
         System.out.println("hello");
   }
}

将打印:
hello
world

Will print: hello world

我真的不明白这个,或者静态代码是什么样的方法。

I don't really understand this, or what kind of method that static code is.

推荐答案

它被称为静态初始化块。

It's called a "static initialisation block".

它在首次加载类时运行;只有一次。

It runs when the class is first loaded; only once.

例如,每次实例化类时都会运行一个构造函数;静态块只运行一次,当它首次由VM / Class加载器静态加载时。

For example, a constructor will run each time the class is instantiated; the static block only runs once, when it's first loaded statically by the VM/Class loader.

这篇关于Java没有名称静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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