search
HomeJavajavaTutorialJava silently loads sample code for Class implementation
Java silently loads sample code for Class implementationOct 18, 2017 am 10:23 AM
classjavaaccomplish

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python中的class类和method方法的使用方法Python中的class类和method方法的使用方法Apr 21, 2023 pm 02:28 PM

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

使用jQuery替换元素的class名称使用jQuery替换元素的class名称Feb 24, 2024 pm 11:03 PM

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

python中class是什么意思python中class是什么意思May 21, 2019 pm 05:10 PM

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

SpringBoot怎么通过自定义classloader加密保护class文件SpringBoot怎么通过自定义classloader加密保护class文件May 11, 2023 pm 09:07 PM

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

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

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

PHP Class用法详解:让你的代码更清晰易读PHP Class用法详解:让你的代码更清晰易读Mar 10, 2024 pm 12:03 PM

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

java的预定义Class对象的方法java的预定义Class对象的方法Jul 01, 2023 pm 06:41 PM

基本的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,怎样解决?Aug 26, 2023 pm 10:58 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

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

Dreamweaver Mac version

Visual web development tools