0

Is there any way to make this code more concise so as not to have so many overloaded methods?

public void method(String[] args1, String[] args2){}
public void method(String[] args1, String args2){}
public void method(String args1, String[] args2){}
public void method(String args1, String args2){}

The number of overloaded methods increases exponentially as the number of arguments increases. This is obviously a problem, and I know there has to be an easier way to do this.

The goal is to find the best way to pass any number of objects of the same type as an argument without using an array for single-object input.

Why? It's for simplicity for the end programmer.

4
  • 2
    We don't know what you're trying to achieve, which makes it hard to help you... could you clarify the question? Commented Jul 24, 2013 at 5:51
  • Ok. Now what is your question here? Commented Jul 24, 2013 at 5:53
  • Isn't public <T> void method(T... args) what you're looking for? Commented Jul 24, 2013 at 7:17
  • No because that would only allow for a string or a string array at one time. Plus, I want multiple arguments. Commented Jul 24, 2013 at 15:41

4 Answers 4

1

Maybe if you have a great number of arguments that - besides - can be increased in future, it's better to wrap them all in separate class, i.e. InputParams and pass only one instance of this class to the method. Consider next code:

InputParam ip = new InputParam();
ip.setField1(field1);
//...
// usage
this.method(ip);

// declaration
public void method(InputParams arg){}

P.S. But, as other guys mentioned, it really depends on many conditions and what you are trying achieve.

Sign up to request clarification or add additional context in comments.

Comments

0

basicly you want to use public void method(String[] args1, String[] args2){} as your main methode and to others just makes it easier for the programmer? You can simple remove the others or still implement them like:

public void method(String args1, String args2){
    this.method(new String[]{args1}, new String[]{args2});
}

In this case you only have to implement the code for public void method(String[] args1, String[] args2){}

Comments

0

Variable argument or varargs in Java allows you to write more flexible methods which can accept as many argument as you need. These functionality enables to write methods/functions which takes variable length of arguments.

public static void main(String args[]) 
{
        test("India");
        test("India", "Delhi");
        test("United States", "New York", "California");
}

public static void test(String... args) 
{
        for(String arg: args) 
        {
            System.out.print(arg+"  ");
        }
}

1 Comment

But alas, varargs can only go at the end of the parameter list.
0

We can create a method by taking all 4 types of arguments and the argument we do not want to send, we can send it as null. In the receiving method as per requirement we can write some logic.

Input : u1.method(null,s3,s1,s4); Here u1 is class object and calling its method

public static void method(String[] args1, String[] args2,String s1, String s2)
{
   System.out.println(s1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.