Use case is something similar to below code. There is a class(Inner_Demo) inside another class(Outer_Demo). Inner_class will be instantiated upon some condition in the outer class private method.
class Outer_Demo {
public Outer_Demo() {
test();
}
// private method of the outer class
private void test() {
Inner_Demo demo;
if(condition)
demo = new Inner_Demo();
}
// inner class
class Inner_Demo {
}
}
main(){
Outer_Demo outer = new Outer_Demo();
// Here I need to check is Inner class got instantiated
// Trying to print the value as below leads to error create
// field/constant Inner_Demo in Outer_Demo
System.out.println(Outer_Demo.Inner_Demo); // outer.Inner_Demo
/* Storing the created instance to Outer_Demo.Inner_Demo
is allowed */
Outer_Demo.Inner_Demo inst = outer.new Inner_Demo();
System.out.println(inst);
}
I need to test, Is inner class is Instantiated or not. I got to know that calling the inner class in above way is incorrect.
Reflection might have used if the field demo in the Outer_Demo class's method test is not local/ have class level access. Can anybody help me to understand, Is there any way find inner class status. Any links to subject is helpful. Thanks.
Aucun commentaire:
Enregistrer un commentaire