java - 谁能解释一下为什么两个代码都按此顺序打印输出的原因(静态关键字执行优先级)
问题描述
代码 1:
public class test {
static {System.out.println("I am here in static");}
public test()
{
System.out.println("I am here in constructor");
}
public static void main(String[] args)
{
System.out.println("I am here in Main");
test staticCheck=new test();
}
输出 1:我在这里是静态的 我在这里是 Main 我在这里是在构造函数中
代码 2:
public class test {
{System.out.println("I am here in static");}
public test()
{
System.out.println("I am here in constructor");
}
public static void main(String[] args)
{
System.out.println("I am here in Main");
test staticCheck=new test();
}
}
输出 2:我在 Main 我在静态我在这里我在构造函数
解决方案
推荐阅读
- php - 使用 MySQL 创建 .htaccess 文件 URL 重写参数
- windows - 同步 2 个 windows 应用程序同时启动
- ios - Swift UICollectionView 在单击时更新单元格标签而不是实时更新
- python - 使用 splinter 如何单击此链接/按钮
- angular - mat-chip-list 中的 Angular Material ngFor - 如何防止换行?
- javascript - 在 HTML5 Canvas 游戏中更改精灵的黑色像素颜色
- python - 如何在不手动设置 FLASK_APP 的情况下通过烧瓶运行或自定义命令运行 Flask 应用程序
- django - 从父级保存相关的子模型字段 - Django
- laravel - Laravel 与表的关系
- php - Timber Twig:按类别查询自定义帖子类型并输出帖子的简码