フィルターのクリア

How do I create an instance of a Java public static nested class from inside MATLAB 7.13 (R2011b)?

3 ビュー (過去 30 日間)
I have a class that contains a public static nested class. How do I create an instance of the static nested class in MATLAB v7.15 (R2011b)?

採用された回答

MathWorks Support Team
MathWorks Support Team 2012 年 5 月 1 日
There are several ways this can be done. The following class Outer, which contains the public static nested class Inner, will be used to illustrate two possible ways:
public class Outer {
public static class Inner {
public int y;
public Inner() {
y = 1;
}
public Inner(int y) {
this.y = y;
}
}
public static Inner makeInner() {
return new Inner();
}
}
  • Add a public static method that returns a new instance of the inner class.
in = Outer.makeInner();
in.getClass().toString()
ans =
class Outer$Inner
  • Use java.lang.Class.getClasses().
%%Create an Instance;
out = Outer();
%%Create a Class object
oc = out.getClass();
%%Create a Class[] containing all the public classes defined within Outer.
oc_public = oc.getClasses();
%%Class object for Outer.Inner
inner_class = oc_public(1);
%%Call a zero argument constructor
in1 = inner_class.newInstance();
%%Call a constructor with arguments
types = javaArray('java.lang.Class',1);
types(1) = java.lang.Integer.TYPE;
in2 = inner_class.getConstructor(types).newInstance(int32(5));

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJava Package Integration についてさらに検索

製品


リリース

R2011b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by