An import question (java.* vs java.awt.*)
Author |
Message |
syntax_error
|
Posted: Mon Sep 17, 2007 7:11 pm Post subject: An import question (java.* vs java.awt.*) |
|
|
As this may sound very basic, however I was wondering what are all the pro/cons of importing
java.* vs any other one such as java.awt.* I do know that there are certain classes and methods in the java.awt.* but why don't we just import all of them?
In advance thank you,
syntax_error |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Euphoracle
|
Posted: Mon Sep 17, 2007 7:21 pm Post subject: RE:An import question (java.* vs java.awt.*) |
|
|
Why use what you don't need? (Also, it's bad practice to do that, if you don't need it, don't import it.)
There's another more technical reason, but I can't remember if it was memory overhead, processing time, or compile time/space usage. |
|
|
|
|
|
rdrake
|
Posted: Mon Sep 17, 2007 7:22 pm Post subject: RE:An import question (java.* vs java.awt.*) |
|
|
Think about it. Let's say in java.X you have a class A. In java.Y, you also have a class A. Now, what happens when you try to create an instance of that class? By importing only the name spaces (or whatever Java folks call them) you need, you do not pollute the name space and remove a lot of confusion. |
|
|
|
|
|
syntax_error
|
Posted: Mon Sep 17, 2007 7:54 pm Post subject: Re: An import question (java.* vs java.awt.*) |
|
|
besides using up more memory ..
what does "instance of that class" mean
srry I just have started java so please explain |
|
|
|
|
|
rdrake
|
Posted: Mon Sep 17, 2007 7:58 pm Post subject: Re: An import question (java.* vs java.awt.*) |
|
|
If you import both java.X and java.Y, and both happen to have class A in them, then Java would get confused over the following:
This would be a problem.
EDIT: Just so it doesn't seem like I'm taking another shot at Java, this would happen in many other languages as well. |
|
|
|
|
|
syntax_error
|
Posted: Mon Sep 17, 2007 8:26 pm Post subject: Re: An import question (java.* vs java.awt.*) |
|
|
okay I do see what you are saying, thank you soo much
for all your help |
|
|
|
|
|
Aziz
|
Posted: Mon Sep 17, 2007 10:50 pm Post subject: RE:An import question (java.* vs java.awt.*) |
|
|
Btw rdrake, packages.
Also, syntax_error, importing java.* would give you nothing. Packages are not like folders. java.* contains no packages. It will not import everything in java.lang, java.awt, etc.
Each class has a package declaration at the top
This is the package. It's just a name. The '.' is only used for organization (well, more, such as where files go, but forget that for now). The package name includes the dot, and it is unique. So the following declaration is not in java.awt:
code: | package java.awt.color; |
Besides, like I said, the package "java" does not have any classes. You will not see the statement "package java;" in the java source code. The standard package for java classes (and the one that is imported by default) is java.lang. Note that this is different from the default package which is the package with no name (everything that doesn't have a "package" declaration at the top goes in here, and most of your classes will be like this when you start). Got it? Probably not, I didn't do a good job of explaining, but it's a start. |
|
|
|
|
|
|
|