• Post Reply Bookmark Topic Watch Topic
  • New Topic

what happens with imported packages from the API at compile time ?  RSS feed

 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I was wondering what happens to the API packages I've imported at compile time. Are they compiled to classes and placed In the same file as the class containing the Import command ?
The reason I'm asking Is because I've noticed the src.zip file Is not In the JRE and since the JRE Is all that's needed to run an app , I'd like to understand what the import command does.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The import command is solely a way to reduce the amount of typing you have to do.
Instead of typing java.util.List (for example) every time you declare a List variable, you can import java.util.List at the top of your file and then just use List in your declarations.
No code is actually imported by the import statement. The compiled standard java classes are included in a file called rt.jar in the JRE's lib directory, so the src.zip file is not needed by the compiler or the runtime JRE.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up.I had a wrong understanding of what Import does.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they have nothing to do with src.zip. The src.zip file shows you what the source code looks like.

There is a file called rt.jar in your installation directories which has all the pre‑compiled .class files in, zipped with an algorithm almost the same as for .zip files. Those files were compiled before you downloaded them. The import declarations tell the compiler how to find a particular class.
import java.util.List;
…means, “If I say List, I mean java.util.List,” and the javac tool is programmed to look in rt.jar for it. Similarly when you execute the bytecode (with the java tool) it will say, “java.util.List,” for List throughout, and the java tool is programmed to load the Class<List> object from rt.jar.
No, the Class objects are all distinct. They are not incorporated into one another. Such inclusion happens in some languages, like C but not in Java®.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!