Solved

Accessing a calling-class' public  method

Posted on 1997-04-29
3
176 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 50 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now