Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone explain these two syntax features

 
William H Taylor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private class WeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {


I got these two questions about the asynctask three types in one and the string ... parameters, I never saw these before. Can someone explain it to me. I was reading through java code and I don't know how to look these up. I think they are Generics but I never saw generics with three types. I also never saw java with a variable set of method parameters. I appreciate the help.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Willima,
Yes, the types inside the <> are generics. They allow you to say that a class works with certain types. You can have more than one generic type in a class. The class itself specifies how many there are. And the AsyncTask docs list three types.

The ... are varargs. From the point of the method, you use it like it is an array. You can call it with an array as well. The key difference is that you don't have to call it with an array. You can call it with 0 or 1 or 2 or 3 or ... Strings and Java will turn it into an array for you when it calls the method.
 
Giovanni Montano
Ranch Hand
Posts: 249
4
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William H Taylor wrote:private class WeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {


I got these two questions about the asynctask three types in one and the string ... parameters, I never saw these before. Can someone explain it to me. I was reading through java code and I don't know how to look these up. I think they are Generics but I never saw generics with three types. I also never saw java with a variable set of method parameters. I appreciate the help.


It is really easy. Under Android the Google Developers preferred to manage background services with more performing tools, so they built AsyncTask as an "helper" for short operations.
You have to look up 2 things the parameters inside the diamond brackets and the methods. I can recognize your code is the one released by google for their udacity course.
It is quite complex, because it is connected to JSON backend, I would suggest you to type yourself a simpler example, focusing on the first parameter called Params that will be passed to the method doInBackground(Params...) and you can leave the other two parameters Void ex <String, Void, Void>. Just remember that you need to call the AsyncTask instance to activate AsyncTask.

 
William H Taylor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand, thank you very much.
 
Stephen M Davies
Greenhorn
Posts: 11
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also important to note, that if you use varargs, they must be the last parameter in a method. So for example, the following method uses var args, and other parameters. The compiler will complain i you try to use the varargs parameter before the end.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic