This article mainly introduces you to the relevant information about java's implementation of silent loading of Class. The article introduces it in great detail through sample code. It has certain reference learning value for everyone to learn or use java. Friends who need it will follow below. Let’s learn together.
Preface
This article mainly introduces the relevant content about java silently loading Class. The reason for this article is because sometimes During development, we have a scenario where we only want to get the basic information of a Class, but do not want to trigger the relevant static code blocks. This method was used in a previous "JAVA Scanning Package" article. I will It's called silent loading. If you have a professional name for it, please correct me.
Generally, when we load a Class, we will use the Class.forName(String name)
method, which will return a Class object and trigger the Static code block, for example:
Let's first write a Bean class that only contains static code blocks.
package com.jinggujin.classloader; public class Bean { static { System.err.println("static code block."); } }
Using the err stream is to output information in time. Using out will cause caching, which may cause the output information to be out of sequence.
Then write Test method to test.
package test; import org.junit.Test; public class ClassLoaderTest { @Test public void test() throws Exception { Class.forName("com.jinggujin.classloader.Bean"); } }
Run and observe the console output:
static code block.
At this time the console will output that we are in static The content printed in the code block proves that using Class.forName(String name)
will trigger the static code block. Then, to achieve silent loading, we cannot use this method. We You can use the loadClass(String name)
method of ClassLoader to load. This method will only load the class without triggering the static code block. Write the same test method for testing.
package test; import org.junit.Test; public class ClassLoaderTest { @Test public void test() throws Exception { Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass("com.jinggujin.classloader.Bean"); System.err.println(clazz.getName()); } }
Run and observe the console output:
com.jinggujin.classloader.Bean
We found that only the class name information we printed was output in the console, while the static The code block is not executed. In addition, we can actually use the overridden method Class.forName(String name)
forName(String name, boolean initialize, ClassLoader loader)
, The effect of silent loading can also be achieved.
Summarize
The above is the detailed content of Java silently loads sample code for Class implementation. For more information, please follow other related articles on the PHP Chinese website!

类和方法的概念和实例类(Class):用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。方法:类中定义的函数。类的构造方法__init__():类有一个名为init()的特殊方法(构造方法),该方法在类实例化时会自动调用。实例变量:在类的声明中,属性是用变量来表示的,这种变量就称为实例变量,实例变量就是一个用self修饰的变量。实例化:创建一个类的实例,类的具体对象。继承:即一个派生类(derivedclass)继承基类(baseclass)的

jQuery是一种经典的JavaScript库,被广泛应用于网页开发中,它简化了在网页上处理事件、操作DOM元素和执行动画等操作。在使用jQuery时,经常会遇到需要替换元素的class名的情况,本文将介绍一些实用的方法,以及具体的代码示例。1.使用removeClass()和addClass()方法jQuery提供了removeClass()方法用于删除

class是python中的一个关键字,用来定义一个类,定义类的方法:class后面加一个空格然后加类名;类名规则:首字母大写,如果多个单词用驼峰命名法,如【class Dog()】。

背景最近针对公司框架进行关键业务代码进行加密处理,防止通过jd-gui等反编译工具能够轻松还原工程代码,相关混淆方案配置使用比较复杂且针对springboot项目问题较多,所以针对class文件加密再通过自定义的classloder进行解密加载,此方案并不是绝对安全,只是加大反编译的困难程度,防君子不防小人,整体加密保护流程图如下图所示maven插件加密使用自定义maven插件对编译后指定的class文件进行加密,加密后的class文件拷贝到指定路径,这里是保存到resource/corecla

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

在编写PHP代码时,使用类(Class)是一个非常常见的做法。通过使用类,我们可以将相关的功能和数据封装在一个单独的单元中,使代码更加清晰、易读和易维护。本文将详细介绍PHPClass的用法,并提供具体的代码示例,帮助读者更好地理解如何在实际项目中应用类来优化代码。1.创建和使用类在PHP中,可以使用关键字class来定义一个类,并在类中定义属性和方法。

基本的Java类型(boolean、byte、char、short、int、long、float和double)和关键字void通过class属性也表示为Class对象;Class类中booleanisPrimitive():判定指定的Class对象是否表示一个基本类型。包装类和Void类的静态TYPE字段;Integer.TYPE==int.class;Integer.class==int.class;数组类型的Class实例对象:Classclz=String[].class;数组的Clas

Vue报错:无法正确使用v-bind绑定class和style,怎样解决?在Vue开发中,我们经常会用到v-bind指令来动态绑定class和style,但是有时候我们可能会遇到一些问题,如无法正确使用v-bind绑定class和style。在本篇文章中,我将为你解释这个问题的原因,并提供解决方案。首先,让我们先了解一下v-bind指令。v-bind用于将V


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
