• Post Reply Bookmark Topic Watch Topic
  • New Topic

import statement takes resources (memory)?  RSS feed

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've been wondering about this:

Let say I only need java.net.URL and java.net.URLConnection.

Is it going to make a difference (resource wise) for these two options:

1st option:

2nd option:


Will the 2nd option import everything into memory? How does import statement work?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the compiler sees "URLConnection" it recognises it as a type (because of its context in the code) but doesn't know where to find it. So the import statement tells it where to look. There is no difference in memory requirements between the two styles, but the 1st option is preferred because
  • There is less risk of collisions (eg Timer; there are two Timer classes available)
  • It is easier to review the code and see what imports have been used.
  •  
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Import statements are a source-only mechanism. The compiler uses them to translate non-fully qualified class names like URLConnection to fully qualified class names like java.net.URLConnection. The generated byte code therefore no longer has any import statements.
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16059
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You could compile your source file with the first way of specifying the imports, save the resulting class file, and then with the second way of specifying the imports. Compare the two class files and you will see that they are exactly the same.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!