A package is a namespace that organizes a set of related classes and interfaces to provide access protection and namespace management.You can think of packages as being similar to different folders on your computer. Software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages. JDK has set of previously defined packages consisting of relevant classes.E.g IO package contains all the classes related to input output. Moreover, many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick, easy and harmless. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would be a nightmare for us. In java we can avoid this sort of problem by using Packages.
Benefits:-
- We can easily determine that the classes and interfaces in a single package are related.
- The names of your classes and interfaces won't conflict with the names in other packages because the package creates a new namespace.
- You can allow classes within the package to have unrestricted access to one another yet still restrict access for types outside the package.
To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. If you do not use a package statement, your type ends up in an unnamed package (Use an unnamed package only for small or temporary applications).
Syntax:
package packagename
e.g:- package MyPackage;
This should be the first statement of the source file before any imports and class declarations.
Example:-
Code:
-----------------------------------------------------------------------------
package MyPackage;
class xx
{
........
.........
}
class yy
{
public static void main(String args[])
{
. . ......
......
}
}
--------------------------------------------------------------------------------
then a directory called MyPackage should be created under the actual directory and the file should stay in that directory. When this particular file is saved it should be saved as yy.java within the directory MyPackage. So when it is compiled the .class files xx.class and yy.class are automatically going to stay in MyPackage directory.
A Hierarchy of packages may be created. To do so, simply each package name should be separated from the one above it by use of a period.
package pkg1[.pkg2[.pkg3]]];
but it should also stay in the corresponding directory.
For eg. a package declared as Package java.awt.image should stay in java\awt\image directory.
Now, again if a particular. java file is made to stay in a directory because it is written as a part of a corresponding package, then the file should be executed accordingly. For eg. the execution of yy.java in the earlier example should be done as
java MyPackage.yy
being in the actual directory i.e path just above the package.
An Example:-
code:
-------------------------------------------------------------------------------
// A simple package
package MyPackage;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name = n;
bal = b;
}
void show( )
{
if(bal < 0)
System.out.print(“-->; ”);
System.out.println(name + “: $ ” + bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance(“K. J. Fielding”, 123.23);
current[1] = new Balance(“Will Tell”, 157.02);
current[0] = new Balance(“Tom Jackson”, -12.33);
for (int i = 0; i < 3; i ++) current[i].show( );
}
}
--------------------------------------------------------------------------------
This file should be called AccountBalance.java and should be placed in a directory named MyPackage. Then after compilation the AccountBalance.class will stay in the MyPackage directory. However, if this does not happen by default then all the .class files should be placed in the MyPackage directory. Then the file should be executed by the command
java MyPackage.AccountBalance
variable is declared private then that variable could be accessed within that class only. It could not be accessed anywhere anyway outside that class. If a variable is made default(not declared public, private or protected) then the variable can be thought to be acting like a private variable outside the package i.e. outside the package that variable cannot be accessed neither from any subclass of that class nor from any other classes. In order to access that variable from outside the package, the variable has to be declared as public or protected. If it is public, then it can be accessed directly from any subclass of that class outside and through an object of that class from some other classes. However if the variable is declared as protected, then it can be accessed outside the package only from any subclass of that class. Moreover, a class has only two possible access levels:- default and public. When a class is declared as public, it is accessible by any other code. If a class has default access, then it can only be accessed by other code within its same package.
No comments:
Post a Comment