Can anyone please tell me the mechanism behind import statement? I know that the statement "import javax.swing.JLabel;" means that we are using JLabel class in our program, but where does the compiler look for this JLabel.class file? I thought that it will be in inside swing folder or javax folder somewhere in jdk7 folder. But I didn't find any. Can anyone explain to me how the compiler does this? Any explanation would be appreciated. Thanks
Nothing you're really good at that you don't do everyday (WS)
The world would be so much better if people talk about other people's virtues as often as they speak about other people's flaws.
the java system classes are packaged in jar file named rt.jar which is located in your java installation directory. also the source of the class files will be in src.zip. when you run the java program the java runtime automatically scans pre-defined folders for these system files. this is the reason you don't specify path for the system classes in your classpath.
Actually, the compiler uses the import statements. It looks first in your current package, then in rt.jar for the java.lang.package, then later for other packages in rt.jar or the rest of your folder structure or your classpath.
This means you can get serious confusion if you create your own classes with the same name as classes in the java.lang package.
You will have a folder, directory or whatever which contains your .java and your .class files. That folder will be in other folders, depending on the package names and structure you have chosen. That is what I meant. You know when you use a file explorer program, on Windows® Windows Explorer, for example, you can see a tree‑like structure when you click on the ⊞ symbols. That wil correspond to the package names and your .class files will sit in a particular directory. The compiler will look through that tree to find the classes you have imported.
If you don’t use package names, which is usual for beginners, you get a very simple folder structure: one folder!