wood burning stoves 2.0
The moose likes Java in General and the fly likes How to update another class's method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to update another class Watch "How to update another class New topic

How to update another class's method?

Ziya Bakan

Joined: Feb 07, 2013
Posts: 13
Hi all!

There are two classes (class Home and class Away). Class Away has a method called update and update method has some arguments(speech,writing,reading) whose values are assigned in class Home. And these values change dynamically┬┤. I have problems at calling this update method from class Home, could you please give me an example? Here is what i tried to do :

public class Away
private void method1(String speech)
//.......definition of the method......

private void method2(String writing)
//.....definition of the method......

private void method3(String reading)
//......definition of the method......

public void update(String speech, String writing, String reading)

method2(speech); // Maybe i should call these methods via an object of the class Away but that s not my problem at the moment!!! but i would be thankful if you could make this clear as well!!!

I tried to call this update method from Home class in the following but i have no clue how to change the arguments of this update method.Because these arguments have dynamic values inside the Home class.Should i create a constructor, or how can i update this "update()"method?

public class Home { .........

Away away_object = new Away();

away_object.update(speech, writing, reading);

Alvin Watkins
Ranch Hand

Joined: May 25, 2011
Posts: 53
I'm not sure I understand your question. Where did you declare "object2" - I'm tired today but am pretty sure I don't see that in your code? I see you made an Away object "Away away_object = new Away()". Why don't you try passing your Speech, Writing and Reading objects to the Away you instantiated such as "away_object.update(speech, writing, reading);"?
Ziya Bakan

Joined: Feb 07, 2013
Posts: 13
I am sorry Alvin that i made a mistake while i was copy-pasting, but now i corrected it as only one away_object created in Home class. So could you please take a look now because this is really important for me!! thanks a lot in advance...
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 225

Why did you define those private methods? It is necessary for your design? I suggest you should keep your instance variable private.
if you want to check the arguments(the three strings that are coming dynamically) then it can be done in update method itself.

Constructor is only used to give the initial value or initialize the instances of a class. to edit the values you may use methods only.

From where these dynamic value are coming?? (text box, console, stream...) ?
what kind of error are you getting in the code?? the code jotted seems ok to me.

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

There are two basic strategies I can think of for this (if I understand the problem):

1) Every time the values change, call the away_object.update(speech, writing, reading) method again. This is the easiest way because it requires no more 'tooling' in your code, you just have to make sure you call the method everytime those values can change.

2) You could employ the Observable/Observer pattern. Create a Listener type of interface for each of the dynamic values. In the Away object, when you receive the three values register a Listener that, when called, updates the Away object's values (or does other necessary work). When the three values change, call all Listeners so they are kept informed of the change. This requires a bit more work to set up but can be pretty powerful for allowing multiple different types of Objects get updates.

I agree. Here's the link: http://aspose.com/file-tools
subject: How to update another class's method?
It's not a secret anymore!