简体   繁体   中英

Dynamic class loading (java 11)

I have the following code

ClassLoader classLoader = IFileTransferClient.class.getClassLoader();
Class f_t_c = classLoader.loadClass(fileGroupConfig.getFileTransferClientClassName());
fileTransferClient = (IFileTransferClient) f_t_c.newInstance();

The compiler (Java 11) is complaining that newInstance is deprecated.

How does one convert the above code for Java 11 compiler?

It is a long standing deprecation of Class#newInstance .

f_t_c.getConstructor().newInstance();

The call above will invoke the normal (in this case: default) constructor, which allows all handling on construction, like exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM