简体   繁体   中英

How to create new object of nested static class in Java with Java Reflection

Suppose I have a "Person.java" with the following code:

//Person.java
public class Person{

    protected Person(String x, String y){
        .....
    } 

    private static class Teacher{

        private Teacher(String x, String y){
            .....
        }

    }
}

and I want to create an Object of static nested class Teacher in "Teacher_A.java"

//Teacher_A.java
import Person

public class Teacher_A {
    public static void main(String[] args) {
        //new Teacher Object here ....
    }
}

How can I do that with Java Reflection?

  1. you can't access Teacher class in Teacher_A because you have defined as below:

    private static class Teacher

A nested class will behave like member of class only , so if your nested class is private then you can access it outside the class.

for more details seeNested Classes

  1. Let say you change access specifier from private to package or public still you can't create object of your Teacher class because you have made constructor as private:

    private Teacher(String x, String y)

From JLS 6.6.1. Determining Accessibility

if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

but anyway if you want to run this change Access Control 1 ex:

package sample;
import sample.Person.Teacher;
public class Teacher_A {
  public static void main(String[] args) {
     //new Teacher Object here ....
      Teacher aa = new Teacher(null,null);
   }
}

package sample;
public class Person{
    protected Person(String x, String y){
           //your code
    } 
    static class Teacher{
          Teacher(String x, String y){
           //your code
          }
    }
}

You can instantiate an inner static class in the following way package.Outerclass$InnerClass . In the below example, it will be com.sample.Person$Teacher

package com.sample;

import java.lang.reflect.InvocationTargetException;

public class Teacher_A {

    public static void main(String[] args) {

        try {

            Class<?> inner = Class.forName("com.sample.Person$Teacher");
            Person.Teacher obj = (Person.Teacher) inner.getDeclaredConstructors()[0].newInstance("Calling ",
                    "Inner Static Class");

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | SecurityException e) {

            e.printStackTrace();
        }

    }

}

class Person {

    protected Person() {

    }

    static class Teacher {

        public Teacher(String x, String y) {
            System.out.println(x + y);
        }

    }

}

Being said that you have made the inner class and its constructor private which will hide it from the outer class. This is should be changed in order to access the inner class from outside and instantiate it.

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