0

I'm a C# developer for many years and used Java several times. And now because of Mobile Development area I am moving to, I commenced to use Java even more.

At this point, it might look like a simple question but I still want to know that in Java we can import a single class or the entire package and in C# we cannot. Aside from reducing extra recommendations in the auto-completion, what good benefit does this "importing one class only" bring?

Tarik
  • 79,711
  • 83
  • 236
  • 349

3 Answers3

9

Wildcard imports have become ill-advised ever since the IDE's support for automatic import management evolved to the point where you never, ever need to write an import statement yourself.

Importing whole packages can, and does, result in name collisions: for example, both java.util and java.awt declare a type List.

Leave the import management to the IDE and don't worry about it.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

The Java coding standards document doesn't seem to have anything to say about this. On the other hand, @Marko Topolnik's comment is contradicted a bit by Netbeans. The most recent versions seem to generate wildcard imports whenever 5 or more classes are imported from a given package and there are no conflicts otherwise. My educated guess is that they went this way because when using big packages, the import list can get extremely long.

Having said that, certainly the best practice is to use an IDE - like Netbeans or (less smoothly in my experience) Eclipse - that takes care of nearly all the import resolution for you.

Edit

During discussion we seem to have figured out that Netbeans 7.1's setting to merge 5 or more to a wildcard was "undone" in later versions.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • In eclipse there are no wildcard imports, which is a change from earlier versions, where there was a threshold. BTW those coding standards are ancient and recommend a whole number of practices which today look ridiculous, like for example declaring all local variables at the start of the method, C-style. – Marko Topolnik Aug 03 '13 at 18:14
  • BTW the pitfall of wildcard imports is that the name resolution can change behind your back---because a conflicting class was added to one of the wildcard-imported packages. – Marko Topolnik Aug 03 '13 at 18:16
  • I wasn't advocating for wildcards, just saying that Netbeans seems to have gone toward them. – Gene Aug 03 '13 at 18:17
  • That would be funny given this quote from the coding conventions for the NetBeans codebase itself: "Prefer explicit imports to wildcards for clarity; if using NetBeans to edit code, the Fix Imports command makes it easy to maintain them. Keep imports sorted." – Marko Topolnik Aug 03 '13 at 18:20
  • Interesting, but nonetheless it's true that Netbeans 7.1 converts to wildcards by default. It does look like there's a way to turn it off. – Gene Aug 03 '13 at 18:22
  • In Netbeans, the threshold above which you go from single to package import is a parameter. You can set it to never (Tools>Options>Editor>Formatting>Language=Java>Category=Import>"Use Single Class Import"). – assylias Aug 03 '13 at 18:22
  • @assylias But what is the factory default? 'Cause I remember the threshold was 3 in earlier Eclipse, and I think it is "never" today. – Marko Topolnik Aug 03 '13 at 18:23
  • @assylias Exactly my problem :) – Marko Topolnik Aug 03 '13 at 18:25
  • No. It's definitely not 0 because I had a codebase with no wildcards and after upgrading to 7.1 without changing defaults the "Reorganize imports" started producing them. Here https://netbeans.org/projects/www/lists/netcat/archive/2011-10/message/64 is an article that says it's 5. – Gene Aug 03 '13 at 18:25
  • @assylias Just checked it, the default threshold in Eclipse is 99. – Marko Topolnik Aug 03 '13 at 18:26
  • 1
    @MarkoTopolnik Netbeans 7.3 and 7.4 beta both default to Single Class Import. – assylias Aug 03 '13 at 18:36
  • @assylias So it was just a glitch in 7.1. That's what I would have expected. – Marko Topolnik Aug 03 '13 at 18:36
  • @assylias Okay good to know. I could delete my answer but maybe someone who went through the same thing I did with 7.1 will find this discussion helpful. – Gene Aug 03 '13 at 18:40
-5

Well, of you only Need One class from a package you should only Import this One class because of you'd Import the whole package it would slow down the java vm.

Edit: Because of the auto completion that is used today it doesn't matter how you import classes.

MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44
  • 6
    This won't be the real issue. Because the imports are just used by Compiler to resolve the classes used. Compiler will replace each class with fully qualified name. So, there is no JVM slow down issue in the scene. – Rohit Jain Aug 03 '13 at 18:07
  • It's not about auto-completion, either; conflicts, convenience, and communication are the reasons, regardless of which side of the debate you're on. – Dave Newton Aug 08 '13 at 13:27