• Post Reply Bookmark Topic Watch Topic
  • New Topic

package issue  RSS feed

 
Neil Mc
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have read that a public class is accessible from everywhere, both inside and outside of the current package.
Why is it then that I have to import, say java.util.* to use an ArrayList when class ArrayList is declared as public ? Should it not be available everywhere?
Thanks.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case, you import to save same typing.
Otherwise you need to use "java.util.ArrayList" to
fully qualify each use of Arraylist.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- this is a good question
But an import statement is not directly connected to access levels...
The *only* reason you need an import statement is because the name of the class ArrayList is NOT really "ArrayList". It's *fully-qualified name* is really "java.util.ArrayList". The import statement is NOTHING more than a way to save you from having to type, everywhere in your code,
java.util.ArrayList.
For example, you can say:
java.util.ArrayList al = new java.util.ArrayList();
OR if you have put in an import statement, you can say:
ArrayList al = new ArrayList();
That's it. The only connection that import statements have to access levels is that they can both be related to packages. But import statements are not about 'visibility'
Often, it LOOKS like import statements are about visibility, because the compiler acts like it cannot *see* the class if you do not have the import statement. But that's because you are not using the correct name! And in fact, without the import statement, and unless you use the fully-qualified name everywhere, the compiler doesn't really know for certain which class you're talking about. In other words, the compiler says, "I can't find a class called "ArrayList"" because there isn't one!
Any class that is in a package has a "full name", which is the package name prepended onto the class name, using the "dot" separator. It is just like saying that my fully-qualified name is "Katherine Jean Sierra". But if someone just calls me "Katherine", my bank doesn't know who that is.
So using an import statement is like someone saying, "Whenever you hear the name 'Katherine', I am talking about 'Katherine Jean Sierra'. As opposed to some *other* Katherine. That is why you can get into trouble sometimes if you use the wildcard import:
import java.util.*;
import java.awt.*;
BOTH of these packages contain a class called 'List'. So if you now say, in your code,
List myList = new List();
The compiler says, "OK, cool, but WHICH List class are you talking about???" In which case, the solution is to use the non-wildcard import, or go ahead and use the fully-qualified name in your code in each place, so that the compiler knows WHICH List you're talking about.
cheers,
Kathy
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!