简体   繁体   中英

How to access a private field in a private field in another class using Java

I am clear that accessing a private field in Java could be easily achieved by using Reflection. As is shown in posts as How to read the value of a private field from a different class in Java? and there are many.

To achieve that , the critical move is to set accessibility.

Field f = obj.getClass().getDeclaredField("aaa"); 
f.setAccessible(true);

But in my case, the situation is like:

class A{
 private B b;

 class B{
   private String value;
 }
}

and I want to get value of abvalue in another class. When I was trying, I intended to do it as

A obj = createInstanceA();
Field f = obj.getClass().getDeclaredField("b"); 
f.setAccessible(true);
A.B b = f.get(obj);
Field f2 = b.getClass().getDeclaredField("value");
f2.setAccessible(true);
String value = f2.get(b);

Which doesn't work out because B could not be declared out of A. Do I have other options if Class A can not be modified?

You have do like this,

public class A {
    private B b = new B();

    class B {
        private String value = "String";
    }
}

public class ClassB {
    public static void main(String args[]) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
        A obj = new A();
        Field f = obj.getClass().getDeclaredField("b"); 
        f.setAccessible(true);
        A.B b = (B) f.get(obj);
        Field f2 = b.getClass().getDeclaredField("value");
        f2.setAccessible(true);
        String value = (String) f2.get(b);
        System.out.println(value);
    }
}

What you are missing is to setAccessible(true) to inner class field.

As a first, in your example field b is null. Is this correct? So, you try to get class of null.

As a second, in your example you use inner classes and there is a specific langugage mechanizm. You can create instance of class B only by some instance of class A. And all instances of class B has access to private field of it's parrent (class A). As in this example.

class OuterClass 
{  
    // static member 
    static int outer_x = 10; 

    // instance(non-static) member 
    int outer_y = 20; 

    // private member 
    private int outer_private = 30; 

    // inner class 
    class InnerClass 
    { 
        void display() 
        { 
            // can access static member of outer class 
            System.out.println("outer_x = " + outer_x); 

            // can also access non-static member of outer class 
            System.out.println("outer_y = " + outer_y); 

            // can also access private member of outer class 
            System.out.println("outer_private = " + outer_private); 

        } 
    } 
} 

// Driver class 
public class InnerClassDemo 
{ 
    public static void main(String[] args) 
    { 
        // accessing an inner class 
        OuterClass outerObject = new OuterClass(); 
        OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 

        innerObject.display(); 

    } 
} 

May be inner classes can solves your problem? (You can read abou it here https://www.geeksforgeeks.org/nested-classes-java/ ) Then reflecsoin is not needed.

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