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 4 of 4

Thread: Why I can't return two variables in one method?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Why I can't return two variables in one method?

    I would like to know why I can't return two variables in one method. I can pass only one variable.

    Eg;

    protected int getData(){
    System.out.print("Enter num1");
    Scanner scan=new Scanner(System.in);
    int num1=scan.nextInt();

    System.out.print("Enter num2");
    int num2=scan.nextInt();

    return num1,num2;

    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Why I can't return two variables in one method?

    It's just the way Java is.

    Typically when you want to return multiple values they are part of some structure - like the x- and y-coordinates of a point - that is handled by returning an instance of a class. If you really want you can return an array of ints.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    netlync (January 14th, 2012)

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Why I can't return two variables in one method?

    When you call the method, how would it know what to return?

    int myNumber = getData();

    What is myNumber? num1 or num2? It can't be both. How would it decide which one to use. Futhermore, once a variable is returned by a method it's flow of control stops and returns to where it came from so if this was possible there would be no way of knowing when it to stop executing.

    It is possible of course, as pbrockway2 said you can create a class that holds two integers and return an instance of the object.

  5. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    netlync (January 14th, 2012)

  6. #4
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Why I can't return two variables in one method?

    Quote Originally Posted by netlync View Post
    I would like to know why I can't return two variables in one method. I can pass only one variable.

    Eg;

    protected int getData(){
    System.out.print("Enter num1");
    Scanner scan=new Scanner(System.in);
    int num1=scan.nextInt();

    System.out.print("Enter num2");
    int num2=scan.nextInt();

    return num1,num2;

    }
    From what I have heard, usually when you want to return more then one variable it is a hint that you need to rethink how you are doing things. I dont know if that is true or not personally, since I have not used any language that supports it (python kind of supports it and I have used that language).

  7. The Following User Says Thank You to Kerr For This Useful Post:

    netlync (January 14th, 2012)

Similar Threads

  1. Need a non void method to return the first three characters of a string
    By fallout87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 9th, 2011, 10:00 PM
  2. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 02:38 PM
  3. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 01:56 AM
  4. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  5. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM