wood burning stoves*
The moose likes Beginning Java and the fly likes what is a package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is a package?" Watch "what is a package?" New topic
Author

what is a package?

pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
hi all,
it may seem a clumsy question to you all,but i want to ask what actually is a package?
i know it is a collection of some files,but is it a conceptual concept only?
or does it make a folder in our current directory?
for example,
i made two files in my current directory named Stuff.java and User.java


package xcom;
public class Stuff{.....}
saved it as Stuff.java

import xcom.Stuff.*;
class User{...}
saved it as User.java

when i compile User.java
it says package xcom.Stuff does not exist.



[ August 30, 2008: Message edited by: Bear Bibeault ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3956
    
  17

A package is an organizational unit. It is reflected in the file system as a directory structure, and in your code as name-spaces.

When you use:
package xcom;
you are declaring this class file to be in the xcom name-space, and implying that the .class file should be found in the xcom directory. The code does not, however, create the directory structure for you. You must do that yourself.

So you should make a new folder called xcom, more the Stuff.java file into it. Then when you compile User it should be able to find the xcom/Stuff.java file, compile that to xcom/Stuff.class and then use the class file to finish compiling User.java.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
And there are options in the javac tool (eg -d) which will create a folder for your package (if it doesn't already exist). More details: Java Tutorial, and javac tool documentation.
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
okay got it. thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Originally posted by pradeepta chopra:
okay got it. thanks
You're welcome
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Then there are some accessiblity rules associated with a package

If you do not explicitly use an access modifier such as public, protected, private the class/constructor/member is said to have "package-private" or "package" or "default" access.

For example if you declare a method without an explicit access modifier, that method will be accessible only to other classes that reside in the same package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is a package?
 
Similar Threads
K&b selftest 10.6 packages
Compilation error
Help on NoClassDefFoundError
I need "-cp ." on my javac command, but the OS classpath includes my current folder
Hibernate 3.3.1 setup help!