0

I have a class A, with a private member int myMember. And a class B with a private member of the class A, called myA;

That is:

public class A{
    private int myMember;
    ...
}

public class B{
    private A myA;

}

I would like to be able to access:

B.myA.myMember;

but it seems I can't because myMember is private in A. The thing is, I need A to be defined as private for the purpose of the exercise (that also includes it can't be protected). Is there a way around this?

Thanks.

elelias
  • 4,552
  • 5
  • 30
  • 45
  • (1) Why are you wanting direct field access? (2) It's irrelevant whether `A` is private; what matters is that `myMember` is private. Can you change that? – chrylis -cautiouslyoptimistic- Sep 08 '13 at 18:38
  • 4
    Use a getter. Use reflection. – Sotirios Delimanolis Sep 08 '13 at 18:39
  • 1
    If your teacher has given this exercise, then probably he wants you to use reflection. Else it is simple, Private is private , you can't access it from outside. – mawia Sep 08 '13 at 18:45
  • See relevant question: http://stackoverflow.com/questions/3567372/access-to-private-inherited-fields-via-reflection-in-java?rq=1 – arjacsoh Sep 08 '13 at 18:48
  • Either use a getter and setter or, probably a better solution in the real world, put the code that needs the value of myMember into a method in class A. – Ernest Friedman-Hill Sep 08 '13 at 21:53
  • Do you want to access private members because its convenient for your code, or because you've been set a "Code puzzle". Unless its a code puzzle using reflection is to completely miss the point of private members (and probably the exercise) – Richard Tingle Sep 08 '13 at 22:20

4 Answers4

5
public class A {

private int myMember;

public int getMyMember() {
    return myMember;
}

public void setMyMember(int myMember) {
    this.myMember = myMember;
}

}


public class B{

private A myA;

public B() {
    myA = new A();
    myA.setMyMember(0);
    int a = myA.getMyMember();
}
}
mustaphahawi
  • 297
  • 1
  • 8
3

Use getters :

public class A {
    private int myMember;
    public getMyNumber() {
        return myNumber;
    }
}

public class B {
    private A myA;
    public A getA() {
        return myA;
    }
}

So now you can code :

B b = new B(); 
b.getA().getMyMember();
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59
  • yes, I thought of that too. But the number of public functions must not increase. I don't know, something's off. Thanks for your answer. – elelias Sep 08 '13 at 20:36
  • perhaps default scope? than it coul be used in the package an you still have a getter. – extraneon Sep 08 '13 at 20:40
2

Since you've stated you can't create more public methods, aka getters, you could use reflection...

public class A{
    private int myMember;
    ...
}

public class B{
    private A myA;

    private int get(){
        try {
             Field field = myA.getClass().getDeclaredField("myMember");
             field.setAccessible(true);
             return (int) field.get(myA);
        catch (Exception e){
             //Something went wrong, the field doesn't exist or a security exception
             return null; //or return some "error number" like -10
        }
    }



}
DeadChex
  • 4,379
  • 1
  • 27
  • 34
1

If you can declare the private field as static then something like this is possible :

public class A {
    private int myMember;
}

public class B {
    public static void main (String[] args) {
        int myMember = new A() {
            public int getPrivate() {
                return myMember;
            }
        }.getPrivate();
        System.out.print("\n\t Id : " + myMember);
    }
}
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59