Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing a calling-class' public  method

Posted on 1997-04-29
3
Medium Priority
?
224 Views
Last Modified: 2010-04-16
           JPanel jp = null;
            try
            {
                  Class  c = Class.forName((String)arg);
                  jp = (JPanel)c.newInstance();
            }
            catch (Exception e)
            {
                  e.printStackTrace();
                  return;
            }
            jp.setProperties(properties);
            jp.init();
            jp.start();

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?
0
Comment
Question by:robertod031297
3 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219958
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.

example:

void class A(){
   static Object Instance;
...
}

void class B() extends A {
   B(){
    }
...
}

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

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

Instance.method();

in your question it would be:

Instance.getInt();
0
 

Accepted Solution

by:
professorC earned 100 total points
ID: 1219959
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.
0
 

Author Comment

by:robertod031297
ID: 1219960
Thanks, I'll have a shot at using your jp.init(this) suggestion.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question