Usage of Class.forName and Class.newInstance

Hello,

I am currently working on the Kernel Green: GitHub - MicroEJ/Kernel-GREEN: This repository contains a Kernel code template that offers basic services for developing Sandboxed Applications.

I am creating a sandboxed application to run on my Kernel and I am facing a question regarding the usage of Class.forName and Class.newInstance methods

I would like to be able to get a Class by its name and then instance it using newInstance method, I am guessing that the type needs to be embedded in the Firmware in some way.

Browsing the only documentation, I found this link: MicroEJ Classpath — MicroEJ Documentation

My question then is: can I make a *.types.list file to specify my Types in the sandboxed application itself or should it be located somewhere in the Kernel instead so I can instanciate it from an app?

Thanks,

Stevann

Hello @stinlot,

The methods Class.forname() and Class.newInstance() are two different things:

  • Class.forname(): to load a type from its name, you have to declare it as a required type. The type names must be declared in a *.types.list file in the same classpath than the declared type. In your case, you have to declare it in the classpath of your Sandboxed Application.
  • Class.newInstance(): the type must be an instantiable class and declare a default constructor with no arguments. See the Class.newInstance() javadoc for more details.

Hope this helps,

–Frédéric