File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java Package Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Package Access" Watch "Java Package Access" New topic

Java Package Access

Castor Tack
Ranch Hand

Joined: Dec 06, 2010
Posts: 31
Hello there:

A package is used to store related classes. And a package is just like a folder.
In eclipse, I create a package "SimpleGUI" in src directory, and create another package "SimpleGUI.model". Actually, "SimpleGUI.model" is a folder "model" in .../src/SimpleGUI

So I should be able to regard everything in folder "model" as something that is also in folder "SimpleGUI". So I should also be able to regard a class in package "SimpleGUI.model" as a class in package "SimpleGUI". (Is this true?)

However, , in package "SimpleGUI", when I try to use a class that is defined in package "SimpleGUI.model", I have to import SimpleGUI.model.classname
Otherwise, type will not be able to be resolved.

Why is that? If it is true that a class in package "SimpleGUI.model" is also a class in package "SimpleGUI" (The logic is: package is folder, so "in sub-folder" means "a part of folder" means "a part of package"), "import" shouldn't be necessary.

The BEST user interface design I've ever seen? --- A flush toilet.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No. If you import a package then (e.g. java.util.*) then the subpackages are not imported. You must import them separately (e.g. import java.util.concurrent.*).

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

And Java conventions say that package names should be all lowercase.

[Asking smart questions] [About Bear] [Books by Bear]
Castor Tack
Ranch Hand

Joined: Dec 06, 2010
Posts: 31
Okay, I'll just keep that in mind.
Thanks, guys
I agree. Here's the link:
subject: Java Package Access
It's not a secret anymore!