Accessing a calling-class' public method

           JPanel jp = null;
                  Class  c = Class.forName((String)arg);
                  jp = (JPanel)c.newInstance();
            catch (Exception e)

The above code extract instantiates a new Class and starts the object.

This new class begins execution and I would now like this class (the newly executed class) to access the callers method (called int getInt ()).

How is this possible?
Who is Participating?
professorCConnect With a Mentor Commented:
You could define the class JPanel to accept a parameter in
it's init method. The parameter would be a pointer to the
object calling init. In your example you would just use
jp.init(this). The JPanel init method could then call any
public method in the object it was passed. You could of course
go the static method route suggested in the comment if
a static method is suitable.

If you didn't want to change init, then you could add a new
method to the JPanel that accepted the callback object.
Finally, if you didn't want to or couldn't modify JPanel you
could extend JPanel adding the necessary method for accepting
the callback object.
One possible solution is to derive both the calling class and the created class from another class that contains a static object member.

Object Instance; // for example

The static object can be assigned a value of this inside the calling class and then the created object coul use it.


void class A(){
   static Object Instance;

void class B() extends A {

void class C() extends A {
        Instance = this;
      // create an instance of B

Now at his point, class B can access a method in class A using:


in your question it would be:

robertod031297Author Commented:
Thanks, I'll have a shot at using your jp.init(this) suggestion.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.