• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Package Statement in Java

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67619
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic