Returning value in a method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Returning value in a method

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Returning value in a method

    Hai guys im a little new bei for java, i know the basics of java, when i m doing some programing practice i face some problems, so please help me with it when i need.
    Thanks
    The problem is how do return 2 values in a method?, at the first place can i do that? pls give me some eg.

  2. #2
    Join Date
    Sep 2006
    Posts
    68
    Hi!

    It's very easy
    1) If you need to return a values of the same type then you should use an array.

    public String[] getSomthing() {
    String[] s = new String[2];
    s[0] = "test1";
    s[1] = "test2";
    return s;
    }

    2) If you need to return a values of the different types then you should use a value object.

    public class CustomVO {
    private String val1;
    private int val2;
    // Add get/set methods
    }

    public CustomVO getSomthing() {
    CustomVO vo = new CustomVO();
    vo.setVal1("test");
    vo.setVal2(10);
    return vo;
    }

  3. #3
    Join Date
    Sep 2006
    Posts
    68
    3) Also you can use a subclasses of a List, Map or Set interfaces.
    public List getSomething() {
    List list = new ArrayList();
    list.add("test1");
    list.add("test2");
    return list;
    }

  4. #4
    Join Date
    Oct 2006
    Posts
    2
    Thanks Uladzimir

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2006, 11:40 PM
  2. Replies: 1
    Last Post: 04-13-2006, 05:57 AM
  3. Replies: 2
    Last Post: 12-31-2005, 06:12 PM
  4. Replies: 2
    Last Post: 07-05-2002, 01:29 PM
  5. Replies: 3
    Last Post: 04-13-2001, 10:13 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center