Return different type of data from a method in java?

前端 未结 13 1500
别那么骄傲
别那么骄傲 2020-12-05 02:18
public static void main(String args[]) {
    myMethod(); // i am calling static method from main()
 }

.

public static ? myMethod(){         


        
13条回答
  •  庸人自扰
    2020-12-05 03:08

    No. Java methods can only return one result (void, a primitive, or an object), and creating a struct-type class like this is exactly how you do it.

    As a note, it is frequently possible to make classes like your ReturningValues immutable like this:

    public class ReturningValues {
        public final String value;
        public final int index;
    
        public ReturningValues(String value, int index) {
            this.value = value;
            this.index = index;
        }
    }
    

    This has the advantage that a ReturningValues can be passed around, such as between threads, with no concerns about accidentally getting things out of sync.

提交回复
热议问题