0

I've been trying to declare multiple strings as parameters to a method without specifying their type for every parameter, however I've not been successful.

What I've got:

private void Fields(String sName, String sSurname, String sMessage) {
   teName.setText(sName);
   teSurname.setText(sSurname);
   teMessage.setText(sMessage);
}

What I want:

private void Fields(String sName, sSurname, sMessage) {
   teName.setText(sName);
   teSurname.setText(sSurname);
   teMessage.setText(sMessage);
}

Of course the second example doesn't work, but it's just to give you an idea of what I mean to do.

Is there a shorter way of doing this?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Abrie
  • 3
  • 3
  • Short answer: no, they are required. – Federico klez Culloca Mar 05 '21 at 12:05
  • longer answer - you could use reflection and shoot yourself in the foot over this idea to assign a parameter named `paramName` to the class's field named `"prefix" + "paramName"` just because you think it's going to save you time or typing. – Shark Mar 05 '21 at 12:08
  • no wait, you want to omit types of the parameters? Have you tried `private void method(String... varArgList)` – Shark Mar 05 '21 at 12:10
  • Take a look at [this question](https://stackoverflow.com/questions/23168342/is-a-variable-length-argument-treated-as-an-array-in-java) for an idea how to do that. – Shark Mar 05 '21 at 12:11
  • 1
    Out of interest, why would you want this? I can only see it bringing pain. Varags could be a solution, but then you would need to maintain the order in which you put the parameters into the method call at all call sites, so hardly ideal. – Gavin Mar 05 '21 at 12:22
  • @shark I've checked the post briefly, thanks for the suggestion. I will try it out. – Abrie Mar 05 '21 at 12:22

1 Answers1

0

You have surely tried to run your code and got a syntax-error. No - it is not possible that way.

But you can do the following:

private void Fields(String[] mydata) {
   teName.setText(mydata[0]);
   teSurname.setText(mydata[1]);
   teMessage.setText(mydata[2]);
}

The call could be:

Fields(new String[] { "whatever", "strings", "you want" });

That way you have to take care of your index and risk an arrayindexoutofbound-exception. But it is possible.

Melvin
  • 925
  • 1
  • 10
  • 22
  • 3
    a vararg list would be better, no? – Shark Mar 05 '21 at 12:10
  • 3
    Note that you would lose the compile time checking of whether you have the correct number of arguments by doing this. – Sweeper Mar 05 '21 at 12:15
  • @sweeper what do you mean by losing compile time? Is there a more efficient way of doing it? – Abrie Mar 05 '21 at 12:24
  • 2
    @Abrie You can pass any number of arguments to the method, and the compiler won't complain. Only at runtime will it throw an exception, if you pass in fewer than 3 arguments. It's not about efficiency. It's not "losing compile time", but "losing compile time checking" i.e. a check that is done at compile time. – Sweeper Mar 05 '21 at 12:25
  • 1
    Oh fair enough. Thanks for the explanation ^^ – Abrie Mar 05 '21 at 12:28