0

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?

2
  • And what is the problem? Get the class, get the constructor, set it accessible, call it. Commented Nov 28, 2019 at 10:09
  • I want to call it Commented Nov 28, 2019 at 10:10

2 Answers 2

1
  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 see Nested 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
          }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

But I know that Java Reflection can manipulate internal properties of the code so I want to using it in this case.
0

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.

1 Comment

I tried but it's not work. Error: "Person.Teacher have private access in com.sample.Person"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.