Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package file and class

 
swapnil paranjape
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
swapnil paranjape
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sherry and swapan..that was of great help...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic