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


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Statement in Java" Watch "Package Statement in Java" New topic
Author

Package Statement in Java

Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
How does the Package Statement work? Like when do I have to specify it. Can someone point me to link/tutorial of where I can find more info about it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61596
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on I/O.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The Package section of the Sun tutorial looks good.

The short answer is we use packages to organize code just like we use folders to organize documents. Large systems can have hundreds or thousands of classes, so careful organization is A Good Thing.

Package structures usually match folder structures on disk or within jar or zip files. When you put "packge x.y.z;" or "import x.y.z.classname;" we expect to find files (source and/or class files) in a folder "x/y/z" relative to one of the locations in the classpath.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
Package structures usually match folder structures on disk or within jar or zip files. When you put "packge x.y.z;" or "import x.y.z.classname;" we expect to find files (source and/or class files) in a folder "x/y/z" relative to one of the locations in the classpath.
=====================================================================================

Thanks a lot Stan. Here's the question though. By default, is our folder the workspace we choose or the the project folder created in that workspace (in which our current class is made)?

Also, (with reference to your example), I thought that if our default folder is x, then we should have access to the classes folders y and z since they be folders in x. SO shouldn't we be able to access the class in them wihtout using the package statement? or do we explicitly have to say

package x.*;

Is it possible to include some other folders outside the current workspace? for instance some other classes from folders outside of X even?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are no shortcuts or implied importing of child packages. So you might see:

BTW: the * here is not recommended. Explicitly listing the classes you import is good documentation and avoids certain problems with duplicate class names. I often code * at first and then use Eclipse's refactoring to fix it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package Statement in Java