This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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?
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);"?
Joined: Feb 07, 2013
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...
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.
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.