-1

Note: I found multiple information about disclose (get) value of protected fields or members of some class, my difference its a need to obtain of instanced objects...

Hi, I want to disclose information the previously instanced static Inner class (Object previously created)...

static public class Outer {
  static class Inner {
    protected int number = 0;
    protected int times = 2;

    public Inner(int number) {
      this.number = number;
    }
  }
}

Outer.Inner oi = new Outer.Inner(8);

I want to print the value of instanced Class, because I don't know how was instanced (what argument was passed).

How?

I was thinking something...

  class Another extends Outer.Inner {
    Another(int j) {
      super(j);
    }

    public void submit() {
      System.out.println("the constructor argument was:" + number);
    }
  }

In other class, of my own project I'm using 'oi', but I need to reveal its information (I don't want to change the original class) and to find where is the object created.

Is there a solution to accomplish this?

Another a = (Another)oi;
a.submit();  // I want to print the original argument.

1 Answers1

1

Best way is probably to override toString method on the Inner class (unless you are already using toString method for another purpose). Then you can just type:

System.out.println(oi.toString())
Noel Lopes
  • 106
  • 6
  • This is an example to explain a complex class, and toString() where I obtain something like: @277c68a5 –  May 30 '17 at 13:51
  • You could do something like this and get what you want (instead of the @277...): @Override public String toString() { return "number = {" + number + "}; Times = {" + times + "}"; } You can read about overriding toString() here: http://javarevisited.blogspot.com/2012/09/override-tostring-method-java-tips-example-code.html#ixzz4iaysj3wS – Noel Lopes May 30 '17 at 20:21