DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  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, 10:40 PM
  2. Replies: 1
    Last Post: 04-13-2006, 04:57 AM
  3. Replies: 2
    Last Post: 12-31-2005, 05:12 PM
  4. Replies: 2
    Last Post: 07-05-2002, 12:29 PM
  5. Replies: 3
    Last Post: 04-13-2001, 09:13 PM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links