Returning 2 values??

acl6804
acl6804 used Ask the Experts™
on
Hi, can anyone tell me is it possible to return 2 values or more in the get Method()??

JavaBean1.java
==============
...
private String abc="";
...
...
public String getAbc() {
     return abc;
     return pk;
}
...
public void setAbc(String lm) {
    abc=lm;
}

Can I do this? If not, how can I return 2 values in the getMethods?? Is it I have to use concta() and append() ??


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
No you can't return two values.
You could define a new class which holds both your objects and use that:

public class AbcPk
{
   String abc;
   String pk;
}

Or simply have two get methods:

public String getAbc() {
    return abc;
}

public String getPk() {
    return pk;
}

Author

Commented:
objects, can I concat() them together and later substring back???

Author

Commented:
objects, can I concat() them together and later substring back???
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Mick BarryJava Developer
Top Expert 2010

Commented:
you can, you just need to decide a way to know where to split them afterwards. Maybe use a seperator that wont be a part of text:

public String getAbc() {
    return abc+"|"+pk;
}

Author

Commented:
object, hm... u got my attention.....

can tell more on this "split" things or not??
The abc +"|"+pk more....

how am i going to "unsplit" thme again?? Is it there are datatype sensitive?? Do them must be same data type??

tell me more...

Mick BarryJava Developer
Top Expert 2010

Commented:
You need some method to unsplit the two values once they are recieved. And personally I wouldn't use this approach.

I don't really understand why you don't just retrieve the two items seperately.
Mick BarryJava Developer
Top Expert 2010

Commented:
If you don't need to split them then just concat them:

public String getAbc() {
   return abc+pk;
}

Commented:
Look if you wish to return multiple values from a method then use Collection. I will fit the same in ur example.
 
public Hashtable getAbc() {
    Hashtable ht = new HAshtable();
    ht.put("ABC",abc);
    ht.put("EFG",efg);
    return ht;
}
...

public static void main (String args[]){
  Hashtable returnTable = getAbc();
  returnTable.get("ABC");
  returnTable.get("EFG");

}
.....
Now u dont have to either concat or split

--
have a nice day
Mick BarryJava Developer
Top Expert 2010

Commented:
That's similiar to one of my original suggestion.
I just defined a specific 2 element collection for the task.

Commented:
Objects suggested the way by maing a new class.
But if there is no need for  new class as it does not hold any significance other than passing the values bak from the method.

One should be judicious while creating objects If some th9ing can be achieved through java API then the developer should not recode for the same unless has very specific requirement.

-choukssa  
Mick BarryJava Developer
Top Expert 2010

Commented:
This actually sounds like a specific requirement :)

Commented:
use return type as String array
it may be easy to use

class Test
{

 public static String[] getAbc()
      {
     String a[] = new String[2];
     a[0] = "one";
     a[1] = "two";
     return a;
     }

 public static void main(String args[]){
      String b[] = new String[2];
      b = getAbc();
      System.out.println("1:" + b[0] + "  2: "+b[1]);
}
}
Java Developer
Top Expert 2010
Commented:
The solution that best meets your needs really depends on your requirements. Though saying that have two methods provides the flexibility for the calling code to use as required. It also saves you creating extra objects everytime the values are required, which could possibly get expensive.

eg. if you had 2 methods like:

public String getAbc() {
   return abc;
}

public String getPk() {
   return pk;
}

you could use in your jsp:

Value of Abcpk is: <%= bean.getAbc() %><%= bean.getPk() %>

or:

Abc is <jsp:getProperty name="bean" property="abc"/>
Pk is <jsp:getProperty name="bean" property="pk"/>
and combined they look like: <jsp:getProperty name="bean" property="abc"/><jsp:getProperty name="bean" property="pk"/>

Author

Commented:
ha... guys so sorry for the late reply.

Well, I think the best methods for us, as a dummy is the most simplest approach.  

TQ for all your times.

:-)


Mick BarryJava Developer
Top Expert 2010

Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial