Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes package file and class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package file and class" Watch "package file and class" New topic
Author

package file and class

swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
In one java file there can be only one public class....we name the java file same as of the java class....now in one file how many packages can be there?
in one package how many public classes can be there?whats the main difference between package and file?
Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83
Originally posted by swapnil paranjape:
...now in one file how many packages can be there?
in one package how many public classes can be there?whats the main difference between package and file?

In java "package" is a way of organizing java classes in order to identify each class uniquely. Having said that you can surely think that a "package" is an hierarchy of folders.
A java file named HelloWorld.java sitting in ...com/idreamz/research folder
should have the package declaration as . And the next thing is to compile and run.

For your questions, you can have only one package declaration. Now if you understand package you can have N (many) classes. Now it should be clear to understand the difference between "package" from a "class".

cheers,
Swapan
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Hello Swapnil,

....now in one file how many packages can be there ?


Packages are nothing more than the way we organize files into different directories according to their functionality, usability as well as category they should belong to.

<b>PACKAGES DO NOT REPRESENT ANY SORT OF SOURCE CODE ORGANIZATION.</b>

There can be only a single package declaration in one java source file. This package can successively have more subpackages.

...in one package how many public classes can be there?


There can be any number of public classes in a package. But ur source file can contain only one public class definition. Import statements help us use the classes defined in that particular package.

...whats the main difference between package and file ?

A package is a way to organize ur source files and classes and interfaces. A file is the place where ur source code is implemented.

Hope this clears ur doubts !!

Cheers !!


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
thank you sherry and swapan..that was of great help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package file and class