• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static imports  RSS feed

 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

So this is the code:



Can someone please explain to me, why I can import static java.util.Arrays.* (or without wildcard .asList; ), and I can't compile Arrays.asList(), but in the other case I can import static rope.Rope.* and compile Rope.swing() ?

Actually my question is my is it possible in the second case. Before this I though I understood the theory. When you have a static import, you don't have to write the name of the class when you are using it.

Thank's in advance!
Alex
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can import only static methods.
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple.
All methods of Arrays are static.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Arrays is missing:

Maybe this one works:
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I tried the code without modification and it compiled and ran first try. Please supply more information about the compiler errors.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not properly understand the following code:

I thought, it does not work; but it seems that I was wrong.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remembering that
List<String> myList = asList("alex", "peter");
is shorthand for
List<String> myList = Arrays.asList("alex", "peter");
have a look at the Arrays#asList() method. Does that documentation help you? Is it the T... bit you are uncertain about?
 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your responses, but when I woke up this morning with a fresh mind, I managed to figure it out what exactly I was missing. For the ones that didn't understood the question, it was about the line:

and the fact that if I wrote it like this: it wouldn't compile. I've tried to make a parallel with Rope.swing(); and the fact that if I wrote just swing(); it would compile.

What I was missing was the fact that for me to actually use the method Arrays.asList(), I had to import java.util.Arrays; . And I didn't do that because in my confusion I thought that the import static java.util.Arrays.*; was enough.


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job figuring it out for yourself and posting your explanation. Have a cow for your effort.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!