• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding importing packages

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any advantage of importing particular class from a package over importing the entire package?
Suppose i need to import class Am from package Alphabets, then does
import Alphabets.Am;
have any advantage over
import Alphabets.*;

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference at run time. Use whichever approach you prefer (or is dictated by the conventions of your workplace).
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose Alphabets package has more than 100 classes.
Then does using

import Alphabets.Am;
over
import Alphabets.*;

increase performance ,won't importing all the classes from packages take more time than importing a single class?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan Deshmkh wrote:Suppose Alphabets package has more than 100 classes.
...
,won't importing all the classes from packages take more time than importing a single class?


As Bear already stated: There is no difference at run time.

You could import every single class in the core API, or you could import nothing at all, and either way, your .class file will be exactly the same.

Imports are only compile-time syntactic sugar. They have zero effect on the generated .class file, and therefore zero effect at runtime.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff : Thanks, i understood.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A single‑class import, however, makes it obvious how many classes you are using from that package. It also reduces the risk of collisions like thisIf you look in those two packages, you find each contains a Timer class ←which is what the compiler said!
 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference at run time. But in general practice use single‑class import , it is easy for code readability.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic