Solved

Accessing a calling-class' public  method

Posted on 1997-04-29
3
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

636 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