• Post Reply Bookmark Topic Watch Topic
  • New Topic

Import Question  RSS feed

 
J. Ryan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone provide an explanation into exactly what happens when you use the import statement? Specifically, do you suffer a performance hit at runtime by doing something like



as opposed to

?

When do classes actually get loaded--when they're first imported, when they're first used, or at some other time? Thanks.
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will only import the necessary classes/modules used in your program. So no, there is not a performance hit. But, for code clarity I was taught (and recommend) to use the specific import. If you are using Eclipse, this is very easy to do. Just let Eclipse import it for you by clicking on the error x and selecting the proper import statement. Not sure about NetBeans, but I imagine it works the same way.
 
Mahesh Bamane
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, in NetBeans also it works the same way.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Brad said, it has no effect on performance at all. This is because imports are not a runtime mechanism - they are only used for the compiler to know where to find each class. You can even omit imports altogether, but that would require you to write the full class name (including package) every time.

In byte code, there is never a reference to File, for instance. Instead, there is a reference to java.io.File - as if there never was any import.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better way to do it in Eclipse: write "arr" then ctrl-space. YOu get a drop-down list, find ArrayList (java.util) on that drop-down list, click it, and Eclipse will do all the importing automatically.

In NetBeans the easiest thing to do is write "java" then you get a drop-down list: click java, then on the next list click util then on the next list click ArrayList.

Or something very similar.
 
J. Ryan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that all makes sense. That was my initial guess, but I just wanted confirmation. Thanks for all the replies.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont imports work in a simular way to referances in the sense that they just point to the API?
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Date;
import java.io.*;

The star form increase the compilation time- especially if you import several large packages. For this reason it is a good idea to explicitly name the classes that you want to use rather than importing the whole packages.However the star form has absolutely no effect on the run-time performance or size of your class.

Reference : The Complete Reference by Herbert Schildt.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!