0

Please find the below code and I'm trying to return 2 objects ( Page6 and Page7 ) but it's asking me to remove the second object. Please let me know how to return multiple objects using return type Object.

Page7.java

public class Page7 {

    public String clsseven() {
      String Val ="Number seven";
      System.out.println(Val);

      return Val;
  }

Page6.java

public class Page6 {

  public String clsSix() {  
    String Val ="Number Six";
    System.out.println(Val);

    return Val;
}}

Page5.java (I'm trying to return about 2 pages here. but it asks me to remove "return new Page7()")

public class Page5 {
  public String clsFive(){
    String Val ="Number Five";
    System.out.println(Val);

    return Val;
}

public Object clsrtn() throws Exception {
    return new Page6();
    return new Page7();
  }
}}
  • 1
    What situation do you need this for? You may receive better advice if you explain your logical problem rather than the code one. – anonygoose Dec 5 '18 at 15:30
1

A method can return at most one value, but it can be an object that has multiple fields - i.e. a "value object"

You can use java collection as below :

public List<Object> clsrtn() throws Exception {     
    List<Object> obj = new ArrayList<>();
    obj.add(new page6());
    obj.add(new page7());
    return obj;
}
| improve this answer | |
  • This is working but I'm not able to Cast array object – Anu Dec 5 '18 at 15:33
0

Logically, a method cannot return more than one value, regardless of language being used. Instead you can change the signature of method to return an array or list of page objects. (As mentioned by NarendraR here). And then access the individual from calling method as:

Page6 objPage6 = list[0];//or array[0], if returned as array.
Page7 objPage7 = list[1];

For your reference: Is it possible to return more than one value from java method

| improve this answer | |
0

As fellows mentioned the method cannot return more than one object because after return statement has executed nothing can be executed after it. However there are few ways how you can return complex object. I'll give you some examples. Say, we have two pages (I override toString() method so that the example is more representative):

class Page6{
    @Override
    public String toString() {
        return "Hi, it's Page6";
    }
};

class Page7{
    @Override
    public String toString() {
        return "Hi, it's Page7";
    }
};

Complex data structures out of the box (Lists, Sets, etc.):

public List<Object> method1(){
    List<Object> result = new ArrayList<>();
    result.add(new Page6());
    result.add(new Page7());
    return result;
}

Arrays:

public Object[] method2(){
    Object[] result = new Object[2];
    result[0] = new Page6();
    result[1] = new Page7();
    return result;
}

Your custom complex data structure:

class MySuperPage{
    Page6 page6;
    Page7 page7;

    public Page6 getPage6() {
        return page6;
    }

    public void setPage6(Page6 page6) {
        this.page6 = page6;
    }

    public Page7 getPage7() {
        return page7;
    }

    public void setPage7(Page7 page7) {
        this.page7 = page7;
    }
}

and then..

public MySuperPage method3(){
    MySuperPage msp = new MySuperPage();
    msp.setPage6(new Page6());
    msp.setPage7(new Page7());
    return msp;
}

If you want to keep your method signature (returning an Object):

Actually (almost) anything in Java is an Object. So you can keep your method returning an Object. The only thing you have to consider is that you will need to cast the result to the particular class that is referred as an Object.

public Object method4(){
    List<Object> result = new ArrayList<>();
    result.add(new Page6());
    result.add(new Page7());
    return result;
}

So below is summing up all the methods in a one main method.

public static void main(String[] args) {

    Main main = new Main();

    for(Object obj: main.method1()){
        System.out.println(obj);
    }

    for(int i = 0; i < main.method2().length; i++){
        System.out.println(main.method2()[i]);
    }

    MySuperPage msp = main.method3();

    System.out.println(msp.getPage6());
    System.out.println(msp.getPage7());

    List<Object> castedObj = (List<Object>) main.method4();
    for(Object obj: castedObj){
        System.out.println(obj);
    }

}

and the output:

Hi, it's Page6
Hi, it's Page7
Hi, it's Page6
Hi, it's Page7
Hi, it's Page6
Hi, it's Page7
Hi, it's Page6
Hi, it's Page7
| improve this answer | |
0

Your class hierarchy is broken - all the other answers here are hacks.

Page7 and Page6 should be of the same base type.

After that, you can return a set of this type:

return new HashSet<BasePage>(Arrays.asList(page6, page7));

If your client expects specific behavior from either page6 or page7, the page object API is misunderstood.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.