How can I access a field of a local inner class from another method?

Consider the field bar of the local-inner class MyValue:

public class C {

public static void main(String x[]) {

    class MyValue implements IValue {
    String bar = "bar";
    public String  getValue() {
        return "my value";
    } 
    }
    MyValue myValue = new MyValue();

    D d = new D();
    d.accessBar(myValue);
}
}      

which implements the IValue interface:

interface IValue {
public String getValue();
}

How can I access the field bar from another function (outside of main), let's say in class D:

class D {
public void accessBar(IValue value) {
    String info = value.getValue() + value.bar;
}   
}
Jon Skeet
people
quotationmark

If you need to access the pass key of a ship and you only have the IShip interface, then IShip should have a getPassKey() method, basically. Even if you could cast to ShipAddress within the method, you shouldn't do so - you should make the parameter type for the calculatedInfo method suitable for all the operations the method requires.

You could access it via reflection, but that would be horribly brittle and I'd strongly recommend that you don't do that.

people

See more on this question at Stackoverflow