• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package Statement in Java

 
Steve De Costa
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve De Costa
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic