Java
javaTutorial
Detailed explanation of how Java implements reflective static loading and dynamic loading of example codeDetailed explanation of how Java implements reflective static loading and dynamic loading of example code
1. The difference between dynamically loaded classes and statically loaded classes in Java
The way new creates objects is called static loading, and using Class.forName(" XXX") is called dynamic loading. The essential difference between them is that the source program of a statically loaded class is loaded during compilation (must exist), while the source program of a dynamically loaded class can be absent during compilation (the source program does not need to exist).
2. Why is it necessary to dynamically load classes
From my own understanding, dynamically loading classes increases the flexibility of the program. For example, there are 50 functions in a program, but you may only use one of them. If you use static loading, you must provide all the definitions of 100 functions before compilation, otherwise the compilation will not pass. If you use It is a dynamic loading mechanism, so there is no need to spend so much time, just define which one to use.
Static loading:
public class Office_Static {
public static void main(String[] args) {
//new 创建对象,是静态加载类,在编译时刻就需要加载所有的可能使用到的类
if("Word".equals(args[0])){
Word w = new Word();
w.start();
}
if("Excel".equals(args[0])){
Excel e = new Excel();
e.start();
}
}
}The two classes Word and Excel must exist when compiling this program. Even if Excel is not used after judgment, it must be loaded
Dynamic loading:
1. Interface OfficeAble:
public interface OfficeAble {
public void start();
}2. Word implementation interface:
public class Word implements OfficeAble{
public void start(){
System.out.println("word start");
}
}3. Excel implementation interface:
public class Excel implements OfficeAble{
public void start(){
System.out.println("excel start");
}
}4. Main method
public class OfficeBetter {
public static void main(String[] args) {
try {
//动态加载类,在运行时刻加载
Class c = Class.forName(args[0]);//在运行配置里面输入加载类.Excel
//通过类类型,创建该类对象(先转换为Word和Excel的共同接口OfficeAble)
OfficeAble oa = (OfficeAble)c.newInstance();
oa.start();
//不推荐下面两种,因为不确定是加载Word还是Excel,要强转
// Word word = (Word)c.newInstance();
// word.start();
// Excel excel = (Excel)c.newInstance();
// excel.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}The above is the detailed content of Detailed explanation of how Java implements reflective static loading and dynamic loading of example code. For more information, please follow other related articles on the PHP Chinese website!
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PMThe article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.
How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PMThe article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PMThe article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra
How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PMThe article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]
How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PMJava's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

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.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.





