0

Possible Duplicate:
Import package.* vs import package.SpecificType

I was wondering what is the difference between

import javax.swing.*

and

import javax.swing.SpecificLibaray

as the first method makes coding a little bit faster and easier?

Is there any performance differences between programs made with entire imported libraries and importing specific library?

What are the advantages and disadvantages of importing libraries the two ways mentioned above?

Regards

Community
  • 1
  • 1
Naruto
  • 1,710
  • 7
  • 28
  • 39
  • http://stackoverflow.com/questions/187453/import-package-vs-import-package-specifictype – Clark Oct 29 '12 at 15:30
  • Java's `import` always struck me as misleading. You can get to `SpecificLIbrary` without any imports whatsoever, you can always specify the full name, eg: `javax.swing.SpecificLibrary.someMethod()`. – NullUserException Oct 29 '12 at 15:30
  • Damn Joachim Sauer just beat me out. – Clark Oct 29 '12 at 15:30
  • Also related: http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – Brendan Long Oct 29 '12 at 15:31

3 Answers3

2

I suppose that you meant to compare wildcard imports with single-class import (and not a library)

Wildcard imports (e.g.import javax.swing.*):

PROS:

  • Easy to use and write

CONS:

  • You may risk to use an innapropriate class (if two classes in different packages have the same name, eg, you want to use java.util.Timer but you have an import javax.swing.* which also contains a Timer class)
  • Compilation is (very) slightly slower

Fully-qualified/single class imports (e.g. import javax.swing.JFrame)

PROS:

  • No risk of confusion with class with identical names.

CONS:

  • If you are not using an IDE, it can be a tedious task to write them all properly.
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
1

Readability - somebody who reads your code knows exactly which classes you're using out of swing. Pretty sure theres no performance hit as java optimizes it before its converted into byte code

Steve's a D
  • 3,801
  • 10
  • 39
  • 60
0

Second one doesn't import a library.It imports a specific class. First one imports all the classes in javax.swing package

prageeth
  • 7,159
  • 7
  • 44
  • 72