0

I am not allowed to alter class A, but i need to get the value of 'a'(set in the constructor). Can anyone tell me how to do that? Every other solution seems to propose a getter in class A. I am not allowed to make changes to class A in any way, no changing to public etc. The magic has to happen in class B.

public class A{
    private int a;
    public A(int x){
        a = x * 10;
    }
}

public class B{
    public B(int x){
        A instA = new A(x);
    }

    public int geta(){
        ???
    } 
}

PS: Posted this question, because every similar question was answered with altering class A, whereas i am not allowed to do so. But since there seems to be only reflection, this question can be closed. Thanks guys and gals.

Peter
  • 13
  • 1
  • 4
  • if member variable is private, the only way to access it outside is via a some method... – Jos Mar 24 '16 at 15:28
  • 3
    You are not allowed to add a getter to A, but are you allowed to modify A in any way? If you are, you could potentially change `private int a;` to `public int a;`. Also, please state WHY you are not allowed to add a getter. – Kevin Cruijssen Mar 24 '16 at 15:28
  • 4
    see: http://stackoverflow.com/questions/1196192/how-do-i-read-a-private-field-in-java – Meiko Rachimow Mar 24 '16 at 15:30
  • Since the options for this (reflection, bytecode hackery, alternate jvm language that doesn't care about access modifiers) are already covered in http://stackoverflow.com/q/1196192/217324, voting to close. – Nathan Hughes Mar 24 '16 at 15:51

3 Answers3

3

That is the whole point of private fields; they are not accessible outside the scope of the enclosing class.

You got the following options:

  1. Change that class to provide a getter
  2. Change your overall design, to either not need that value; or find another way to make it available to both classes
  3. Not recommended: use reflection and its ability to override the "private" protection at run time
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks. I understand the point of private fields. I guess the wording of the excercise is wrong. I don't think that reflection is the solution we are supposed to use. I was hoping someone would point me here to a solution i was just unable to see. I think we are not allowed to change the given methods, but add some, otherwise there seems only to be reflection... – Peter Mar 24 '16 at 15:49
0

The best decision is to have a getter. It's the safest and cleanest solution when it comes to programming best practices.

It's possible to achieve what you want using reflection - you can find the field by name (what if the name changes in a future version of the class?) and the extract it's value like this:

A a = new A(5);
Field field = A.getField("a");
field.setAccessible(true);
int value = field.get(a);

But bear in mind that this is considered super hacky and it strongly advice against.

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
0

Here is how you can do it

    public int geta(){
        Field field;
        int x = 0;
        try {
            field = A.class.getDeclaredField("a");
            field.setAccessible(true);
            x = field.getInt(this.a);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    System.out.println(x);
    return x;
}
Alok
  • 21
  • 2