jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Question about packages. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about packages." Watch "Question about packages." New topic
Author

Question about packages.

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hello Fellow Ranchers,
I'm having an issue with knowing how class access works when classes are in different packages.
The question I have is:
If I have an interface in one package(app.pack1), and that interface is public:

And I have a class that implements the Interface in another package(app.pack2).

Why do I have to import it(import app.pack1.MyInterface...) If the interface is public?

Could someone please explain to me these basics about packages?

Thank in advance,

Sincerely,
Jose
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Consider this for example, you want to use class ArrayList that is located in the package java.util if you didnt specify with import the package to search then the Compiler and JVM would have to search the entire packages which would take long time to find the appropriate class.

So using import statements you tell the Compiler and JVM where to find classes.
[ October 01, 2007: Message edited by: Ahmed Yehia ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Another fine example: List.

Now, which one would you like? java.awt.List (a GUI element) or java.util.List (the interface)? Without explicitly importing it (with or without wildcards), the compiler doesn't know which one you need.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hello
I've read the responses, and I have to say I'm very thankful.
So, I understand now that classes or interfaces that will be used or implemented in a class must be imported or the compiler will not know which one to use. and this happens when classes or interfaces are in different packages.

So, Do the same rules apply when everything is on the same package? or how does it work in that case?

As always, keep up the fantastic work,
and god bless you all
Jose
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Originally posted by Jose Campana:
So, Do the same rules apply when everything is on the same package? or how does it work in that case?

Classes and interfaces from two locations are automatically imported, and do not need to be explicitly imported:
- the same package
- the java.lang package

Of course you still can import them, but you don't have to.
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
Originally posted by Jose Campana:
Hello Fellow Ranchers,
I'm having an issue with knowing how class access works when classes are in different packages.
The question I have is:
If I have an interface in one package(app.pack1), and that interface is public:

And I have a class that implements the Interface in another package(app.pack2).

Why do I have to import it(import app.pack1.MyInterface...) If the interface is public?

Could someone please explain to me these basics about packages?

Thank in advance,

Sincerely,
Jose



It is not necessary to use import in order to use an interface or class from a different package. One can use a public class or an interface from any package by using the fully qualified class name.
So in a java file an import statement is simply a declaration of a shortcut
to indicate that you would like to use the class name alone instead of the
fully qualified class name through out a given java file.


The Zen of Java Programming.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Mr. Zen of Java Programming, thank you very much...

This will help me in my java training.

God bless.

sincerely, Jose
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about packages.