Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is a package?

 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay got it. thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pradeepta chopra:
okay got it. thanks
You're welcome
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic