aspose file tools*
The moose likes Java in General and the fly likes Calling a method in object outside of your scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling a method in object outside of your scope" Watch "Calling a method in object outside of your scope" New topic
Author

Calling a method in object outside of your scope

Morrie Segal
Greenhorn

Joined: Jun 22, 2009
Posts: 13
The environment: I have three classes A, B and C. Each is defined in separate .java files. They all extend JPanel. A1 is my Public class. A1 sets up a JTabbedPane. A1 adds B1 and C1 as separate tabs in A1's JTabbedPane.

The problem: Periodically B1 needs to add a row to a JTable on C1.

The Question: How can B1 address a method that update the JTable in C1?
Morrie Segal
Greenhorn

Joined: Jun 22, 2009
Posts: 13
I got passed the initial issue, but it required me to set the method in C1 to static. Why?
I guess you can close this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
The most likely cause for a request to label something static is that you are at present in a static context. The static context might not be appropriate; maybe you should be in an instance context.
Morrie Segal
Greenhorn

Joined: Jun 22, 2009
Posts: 13
I'm not sure how to fix this. I assume that an instance context is referring to an objet instance. What does a static context refer to?
Thanks, for taking the time to educate me on this nuance.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Morrie Segal wrote:I'm not sure how to fix this. I assume that an instance context is referring to an objet instance. What does a static context refer to?
Thanks, for taking the time to educate me on this nuance.


A static context means you are calling a method on a class but don't have/need a reference to an instance of that class.

If you wanted to keep the method an instance method (non-static) then you would need to pass a reference to C1 into B1.

So you said you now do this:
1) You have an Object A1 which makes a tabbed pane
2) A1 makes an Object named B1 and adds it to the tabbed pane
3) A1 makes an Object named C1 and adds it to the tabbed pane

You would need to add:
4) A1 passes the reference to C1 into B1

B1 would have a method which takes a reference to C as a parameter and stores it. Then when B1 needs to update the table it calls the C object's method.


An alternative is to isolate the data (called a Model) from C and pass the Model to both B1 and C1. B1 updates the Model and C1 reacts to updates. This helps keep B1 from being dependent on C1 (the less tightly coupled your GUI components are the better). See this tutorial and the TableModel JavaDocs for how to use.


Steve
Morrie Segal
Greenhorn

Joined: Jun 22, 2009
Posts: 13
I think I get it. I missed the point that there may be multiple C1 instances. The compiler does not know which one I am addressing. The update from B1 could have one of two contexts:

1. Update a variable in a specific instance of C1 (instance).
2. Update a variable that is shared between all C1s (static).

In the first version, B1 needs to know which C1 to address. In the second version I need to define the method and target variable as static so that all C1 instances see the update.

The model concept provides an abstraction layer between the target variable and both the B1 and C1 instances. I'l have to try that next.

Thanks for taking the time to teach.
--morrie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling a method in object outside of your scope