Home > Java > javaTutorial > body text

Java silently loads sample code for Class implementation

黄舟
Release: 2017-10-18 10:23:33
Original
1664 people have browsed it

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.");
 }
}
Copy after login

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");
 }
}
Copy after login

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());
 }
}
Copy after login

Run and observe the console output:

com.jinggujin.classloader.Bean
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template