Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: share data from methods

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default share data from methods

    Hi all and sorry for my question :-(

    I've this sample code:

    public class ShareVar {
    public static void main(String[] args) {

    System.out.println("Number is: "+c);

    }

    private static int Calc(){
    int a=10; int b=3; int c;
    c=a+b;
    return c;
    }
    }

    variable c is not shared to main class

    How allow c be share in whole code?

    thanks!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: share data from methods

    wow! I'm going to study... very thanks!

Similar Threads

  1. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  2. How EAR packages and beans can share session id?
    By rcbandit2 in forum Java Theory & Questions
    Replies: 0
    Last Post: December 25th, 2011, 11:31 AM
  3. Share your story with Java?
    By imicrothinking in forum The Cafe
    Replies: 6
    Last Post: October 25th, 2011, 06:32 AM
  4. How to share variable values amongst different classes?
    By igniteflow in forum Object Oriented Programming
    Replies: 8
    Last Post: August 20th, 2009, 08:53 AM