Import Package.* VS Import Package.Specifictype

Import package.* vs import package.SpecificType

There is not a performance or overhead cost to doing import .* vs importing specific types. However, I consider it to be a best practice to never use import .* My primary reason for this is I just like to keep things straightward, clean and with as little ambiguity as possible, and I think with a .* import you lose that.

Why is using a wild card with a Java import statement bad?

The only problem with it is that it clutters your local namespace. For example, let's say that you're writing a Swing app, and so need java.awt.Event, and are also interfacing with the company's calendaring system, which has com.mycompany.calendar.Event. If you import both using the wildcard method, one of these three things happens:

  1. You have an outright naming conflict between java.awt.Event and com.mycompany.calendar.Event, and so you can't even compile.
  2. You actually manage only to import one (only one of your two imports does .*), but it's the wrong one, and you struggle to figure out why your code is claiming the type is wrong.
  3. When you compile your code, there is no com.mycompany.calendar.Event, but when they later add one, your previously valid code suddenly stops compiling.

The advantage of explicitly listing all imports is that I can tell at a glance which class you meant to use, which simply makes reading the code much easier. If you're just doing a quick one-off thing, there's nothing explicitly wrong, but future maintainers will thank you for your clarity otherwise.

How to import java class?

You can import the general package, but it's better to be more explicit and import the specific classes you need. It helps to prevent namespace collision issues and is much nicer.

Also, if you're using Eclipse and the shortcut CTRL+SHIFT+O it will automatically generate the explicit imports, prompting you for the ambiguous imports.

which is better in terms of perfomance?

It makes no difference at all at execution time. The compiled code will be identical - and it will also be identical to the equivalent code with no imports, but every type name specified in full at every point in the code.

It will almost certainly make no significant difference at compile time either.

Personally I use a wildcard import when writing throwaway code by hand (e.g. for samples on Stack Overflow) but get Eclipse to import specific classes when writing real code.

Import package.* or Import package.class_name

While there isn't a runtime performance hit, it is bad practice to use *

Reason being as an outsider reading your code, I want to know EXACTLY which class you are using. There often are a number of classes that are implemented in different ways in different packages. Avoiding * makes it obvious which you are using.

For example,
java.awt.List;
vs
java.util.List;

It is very possible you will get obscure naming collisions if you rely on *.

Having said that, all the * does is tell the java compiler where it should look to resolve class definitions. So saying * says, "hey compiler, when looking for classes feel free to look anywhere that matches the pattern blah.blah.*" So in theory, there is a VERY minimal performance hit in compilation when making the compiler search this pattern for the class. But, you would never notice it, and this hit is totally negligible.

Which would make a Class file bigger? import java.awt.*, or a bunch or single import statements?

No difference. These are a compile-time construct only.



Related Topics



Leave a reply



Submit