Solved

Accessing a calling-class' public  method

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 19
groupNoAdj 7 95
Java Jpanels and Jframe 8 33
ForLoop Example 3 48
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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