Forums Register Login

Can someone explain these two syntax features

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
I understand, thank you very much.
+Pie Number of slices to send: Send
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.


Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 762 times.
Similar Threads
Variables afer thread are not being display in the activity?
Issue so far is 2 days in that, i need reload fragment data after made refresh token using asynctask
String[] IN doInBackground? AND RETURN THIS..??
If the server is working couple of minutes or hours client freeze every X seconds
Reading from a socket
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:34:04.