Results 1 to 5 of 5
  1. #1
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default Method to return values

    Hello. I wrote a method to return the values of the student name and the student age. How can I write a single method to return the values of a String and a int.

    Thanks




    Java Code:
    public class School
    {
    
    private String StudentName;
    private int StudentAge;
    
    public void setStudentName(String Name)
    {
        StudentName = Name;
    }
    public String getStudentName()
    {
        return StudentName;
    }
    public void setStudentAge(int Age)
    {
        StudentAge = Age;
    }
    public int getStudentAge()
    {
        return StudentAge;
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by puchatek View Post
    Hello. I wrote a method to return the values of the student name and the student age. How can I write a single method to return the values of a String and a int.
    Methods can only return one value but they can return a value of any type so this would be a valid return type:

    Java Code:
    class ReturnType {
       public int ival;
       public String sval;
    }
    You can enhance this little class ad lib but I hope you get the idea.

    kind regards,

    Jos

  3. #3
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by puchatek View Post
    Hello. I wrote a method to return the values of the student name and the student age. How can I write a single method to return the values of a String and a int.

    Thanks




    Java Code:
    public class School
    {
    
    private String StudentName;
    private int StudentAge;
    
    public void setStudentName(String Name)
    {
        StudentName = Name;
    }
    public String getStudentName()
    {
        return StudentName;
    }
    public void setStudentAge(int Age)
    {
        StudentAge = Age;
    }
    public int getStudentAge()
    {
        return StudentAge;
    }
    }
    Quote Originally Posted by JosAH View Post
    Methods can only return one value but they can return a value of any type so this would be a valid return type:

    Java Code:
    class ReturnType {
       public int ival;
       public String sval;
    }
    You can enhance this little class ad lib but I hope you get the idea.

    kind regards,

    Jos
    Would it be possible to do it in the same class?

    Thanks

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by puchatek View Post
    Would it be possible to do it in the same class?

    Thanks
    Sure, you can make the ReturnValue class a nested class (the Student class will be the top level class) and make it public so the entire world can work with it:

    Java Code:
    public class Student {
       public static class ReturnValue {
          int ival;
          String sval;
       }
       // methods that return a ReturnValue go here ...
    }
    ...
    // somewhere else in your code:
    Student s= new Student();
    Student.ReturnValue retval= s.someMethod();
    // do something with retval.ival or retval.sval ...
    kind regards,

    Jos

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    In this case, though, it would be a bit pointless since those two values are the same two values held in School (shouldn't that be Student?).

Similar Threads

  1. Replies: 0
    Last Post: 11-03-2010, 04:35 PM
  2. java method with multiple return values
    By Ms.Ranjan in forum New To Java
    Replies: 1
    Last Post: 06-18-2009, 06:08 PM
  3. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 04:07 AM
  4. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •