Re: share data from methods
If this were my assignment, I'd call the calc() method (note that since it's a method, its first letter should be lower-case not upper-case) inside of the main method, and then I'd display the value that it returns.
Re: share data from methods
thank you :-) I need use the variable c by main code and not print it. (This code in only sample) - So I need use that variable in the main code.
Re: share data from methods
Please print your actual assignment, word for word. Let's see exactly what you have to do before giving incorrect advice.
Re: share data from methods
Ok (and sorry for my english) I build an android app and I've an auto-refresh textview (its get external ip address every 5 seconds).
The method that check external ip is same of calc() method. In the main code I need handle that variable (like c) in other way but c in not available.
Thanks!
Re: share data from methods
I would suggest that you use an observer pattern to achieve this, and something like this can be done easily using PropertyChangeSupport and PropertyChangeListeners:
- Make the field listened, the one that holds the external ip value, to a "bound property", say called externalIp.
- That is, make it a private field of the class: private String externalIp = "";
- Give this class a public String getExternalIp() method that returns the current value of externalIp.
- Give the class a PropertyChangeSupport instance
- Give the class both an addPropertyChangeListener(PropertyChangeListener listener) and a removePropertyChangeListener(PropertyChangeListene r listener), and add or remove the listener from the PropertyChangeSupport instance in these methods.
- Give your class a public setExternalIp(String newValue) method.
- Inside of this method, call firePropertyChange(propertyName, oldValue, newValue) after setting the newValue to notify the listeners.
- Then any object that wants to listen to and respond changes in the externalIp value can add a PropertyChangeListener to the object of this class, and then get the newest externalIp String whenever it has been notified of change.
Re: share data from methods
wow! I'm going to study... very thanks!