MATLAB: How to access a Java inner class from Matlab

inner classjavaMATLAB

As described in this MathWorks support node, it is not straightforward to access inner Java classes from Matlab…

Best Answer

However, contrary to the answer provided by support, this is possible without writing custom wrappers!
If you are trying to get an inner class that's an Enum class, because you need the Enum values, chances are you can get away with something like (credit Bob Gilmore):
NORMAL = javaMethod('valueOf', 'javax.swing.JTable$PrintMode', 'NORMAL')
For trickier situations, the best I've come up with so far is:
InnerClass = java.lang.Class.forName('package.OuterClass$InnerClass', true, classloader);
In order to get a classloader object that knows how to find your InnerClass, the best I've come up with so far is to start with a trivially constructable object from the same package, get that instance's class, then get the classloader from that:
trivial_instance = package.TrivialClass();
TrivialClass = trivial_instance.getClass();
package_class_loader = TrivialClass.getClassLoader();
Hopefully there's a better way out there...
Once you have the InnerClass object you can call newInstance() on it to make an instance. If InnerClass is an Enum, you can also use getFields() to get a list of the possible values.