当前位置: 首页 > news >正文

java类中静态块_java – 为什么.class不调用类中的静态块?

这是我有的代码:

public class StupidClass {

static {

System.out.println("Stupid class loaded!");

}

}

和我有的测试,我单独运行。

import org.junit.Test;

public class StupidTest {

@Test

public void foo() throws ClassNotFoundException {

final Class> stupidClass = Class.forName("StupidClass");

System.out.println(stupidClass.getSimpleName());

}

@Test

public void bar() throws ClassNotFoundException {

final Class stupidClassClass = StupidClass.class;

System.out.println(stupidClassClass.getSimpleName());

}

}

当我运行测试foo我会看到:

Stupid class loaded!

StupidClass

但是当我运行测试栏时,我看到的是:

StupidClass

报价从this页..

Class objects are constructed automatically by the Java Virtual

Machine as classes are loaded and by calls to the defineClass method

in the class loader.

所以我的理解是,在测试栏,Stupid类加载,否则我会看到一个null我猜?所以Class对象被创建,因为类本身被加载..

现在引用从this页

Static initialization blocks are run when the JVM (class loader – to

be specific) loads StaticClass (which occurs the first time it is

referenced in code).

所以我期待看到“愚蠢的类加载!文本在测试栏中,但我不是。

Each of the classes Candy, Gum, and Cookie has a static clause that is

executed as the class is loaded for the first time.

这是不是很准确的似乎..

我缺少什么?

相关文章:

  • java referrer_JavaScript中document.referrer的用法详解
  • java代码压缩文件_使用Java代码压缩文件或文件夹
  • 石子合并 动态规划 java_动态规划:圆形石子合并问题
  • java修饰方法_Java 修饰符
  • arduino timer频率_Arduino利用TimerOne库产生固定频率和占空比的方波
  • flask数据库mysql增删查改_flask_sqlalchemy简单增删查改操作
  • java基础语法的意义_关于java基础语法的学习笔记
  • java中容器试题_Java最常见208道面试题_容器
  • java 开启线程扫描程序_当多个线程在Java中使用System.in上的扫描仪...
  • java中结构体实现_JAVA中如何实现C中的结构体数组的功能?
  • java nio close_wait_Java NIO 操作总结
  • java zmq订阅_java zmq消息队列
  • java按键数据库添加_详解Java MyBatis 插入数据库返回主键
  • java ee jdbc_JavaEE JDBC 补充注意点
  • java 返回前台excel_Java后台读取excel表格返回至Web前端
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • Java应用性能调优
  • laravel 用artisan创建自己的模板
  • Map集合、散列表、红黑树介绍
  • MySQL数据库运维之数据恢复
  • Protobuf3语言指南
  • Python实现BT种子转化为磁力链接【实战】
  • Python学习之路13-记分
  • Redis 懒删除(lazy free)简史
  • Solarized Scheme
  • 高程读书笔记 第六章 面向对象程序设计
  • 双管齐下,VMware的容器新战略
  • 学习笔记:对象,原型和继承(1)
  • No resource identifier found for attribute,RxJava之zip操作符
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #Linux(权限管理)
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (C++20) consteval立即函数
  • (Python第六天)文件处理
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (独孤九剑)--文件系统
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)jQuery 基础
  • .form文件_一篇文章学会文件上传
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .net6Api后台+uniapp导出Excel
  • /bin/bash^M: bad interpreter: No such file or directory
  • [ C++ ] STL---string类的使用指南
  • [100天算法】-二叉树剪枝(day 48)
  • [2018-01-08] Python强化周的第一天
  • [acm算法学习] 后缀数组SA
  • [Android] Upload package to device fails #2720
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)
  • [BT]BUUCTF刷题第9天(3.27)
  • [CodeForces-759D]Bacterial Melee
  • [HOW TO]如何在iPhone应用程序中发送邮件