Solved

Accessing a calling-class' public  method

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

710 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