Java 我们什么时候使用静态和动态类加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19519787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:59:12  来源:igfitidea点击:

When do we use Static and Dynamic class Loading?

javaclass

提问by Madhu Sudhan Reddy

I know the difference between Static class loading and Dynamic class loading. In general,we always use Static class loading only.Can anyone tell, under which situations we use Dynamic class loading??

我知道静态类加载和动态类加载之间的区别。一般来说,我们总是只使用静态类加载。谁能告诉我们,我们在什么情况下使用动态类加载??

采纳答案by nilesh.b

Dynamic Class Loading allows the loading of java code that is not known about before a program starts. The Java model loads classes as needed and need not know the name of all classes in a collection before any one of its classes can be loaded and run.

动态类加载允许在程序启动之前加载未知的 Java 代码。Java 模型根据需要加载类,并且在加载和运行任何一个类之前不需要知道集合中所有类的名称。

For example : Depending on user input you want to create only one object and there are hundreds of classes. Then you don't need load all classes. You can create object at run time by dynamic class loading.

例如:根据用户输入,您只想创建一个对象,并且有数百个类。那么你不需要加载所有的类。您可以通过动态类加载在运行时创建对象。

Code:

代码:

try {
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(in);

    System.out.println("Enter Class Name: ");
    String whatClass = reader.readLine();

    Class exampleClass = Class.forName(whatClass);
    Object ob = exampleClass.newInstance();

} catch (ClassNotFoundException e) {
    e.printStackTrace();

} catch (Exception e) {
    e.printStackTrace();
}

回答by Antoniossss

When you are using reflection and creating new instances. You can always fetch new jar eg. via url and create object from it on runtime.

当您使用反射并创建新实例时。你总是可以获取新的罐子,例如。通过 url 并在运行时从中创建对象。

回答by Abhijith Nagarajan

One common example is trivial JDBC programming. Dynamic classloading is used to load the driver class

一个常见的例子是简单的 JDBC 编程。动态类加载用于加载驱动类

If you see any code with Class.forName() then that is the example for dynamic loading

如果您看到 Class.forName() 的任何代码,那么这就是动态加载的示例

回答by Kayaman

Generally speaking, whenever your program will use classes that aren't necessarily available at compile time. An example would be a plug-in system, where you could create new plug-ins without recompiling the original application.

一般来说,只要您的程序将使用在编译时不一定可用的类。一个例子是插件系统,您可以在其中创建新插件而无需重新编译原始应用程序。

回答by Grzegorz ?ur

Most common case in Java are plugins and plugin like libraries like JDBC drivers.

Java 中最常见的情况是插件和插件之类的库,如 JDBC 驱动程序。

回答by Prateek

DYNAMIC CLASS LOADING

动态类加载

It allows you to build your applications so that key external dependencies are not compiled into the application source-code.

它允许您构建应用程序,以便关键的外部依赖项不会编译到应用程序源代码中。

APPLICATIONS

应用

JDBC

JDBC

For example, in the JDBC case, it allows you to switch between different driver implementations, and (in theory) different database vendors without changing your source code.

例如,在 JDBC 情况下,它允许您在不同的驱动程序实现和(理论上)不同的数据库供应商之间切换,而无需更改源代码。

PLUG-INS

插件

Another use-case is when some supplier develops a generic form of an application with extension points that allow customers to "plug in" their own custom classes. The custom classes are typically loaded using Class.forName(...).

另一个用例是当某些供应商开发具有扩展点的应用程序的通用形式时,允许客户“插入”他们自己的自定义类。自定义类通常使用 Class.forName(...) 加载。

FRAMEWORKS AND CONTAINERS

框架和容器

A third use-case is application frameworks and containers which typically use Class.forName(...) under the hood to dynamically load the classes for application-specific beans, servlets, and so on.

第三个用例是应用程序框架和容器,它们通常在后台使用 Class.forName(...) 来动态加载特定于应用程序的 bean、servlet 等的类。

OTHERS

其他

A fourth use-case is where the application (or more likely an application library) has modules that are not used in a typical application run. By using Class.forName(...) internally, the application or library can avoid the CPU and memory overhead of loading and initializing large numbers of unwanted classes. (The Sun Swing libraries apparently do this to reduce application startup times, and I'm sure there are other examples.)

第四个用例是应用程序(或更可能是应用程序库)具有典型应用程序运行中未使用的模块。通过在内部使用 Class.forName(...),应用程序或库可以避免加载和初始化大量不需要的类的 CPU 和内存开销。(Sun Swing 库显然这样做是为了减少应用程序启动时间,我相信还有其他示例。)

Refer Dynamic Class Loading

参考动态类加载

回答by sunny navani

Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically by using Class.forName (String className); method, it is a static method.

动态加载是一种在运行时以编程方式调用类加载器功能的技术。下面我们来看看如何使用 Class.forName(String className) 动态加载类;方法,它是一个静态方法。

The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Once the class is dynamically loaded the class.newInstance () method returns an instance of the loaded class. It is just like creating a class object with no arguments.

上面的静态方法返回与类名关联的类对象。字符串 className 可以在运行时动态提供。一旦类被动态加载,class.newInstance() 方法将返回一个已加载类的实例。这就像创建一个没有参数的类对象。

A ClassNotFoundException is thrown when an application tries to load in a class through its class name, but no definition for the class with the specified name could be found.

当应用程序尝试通过类名加载类,但找不到具有指定名称的类的定义时,会抛出 ClassNotFoundException。