-
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.
-
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) 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;
}
-
Similar Threads
-
Replies: 2
Last Post: 08-05-2006, 10:40 PM
-
Replies: 1
Last Post: 04-13-2006, 04:57 AM
-
By seefrank in forum Java
Replies: 2
Last Post: 12-31-2005, 05:12 PM
-
Replies: 2
Last Post: 07-05-2002, 12:29 PM
-
Replies: 3
Last Post: 04-13-2001, 09: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
Forum Rules
|
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
|
Bookmarks