Java Packages
Consider a situation where in a user has about fifty files of which some are related to sales, others are related to accounts, and some are related to inventory. Also, the files belong to different years. All these files is required, the user has to search the entire cupboard. This is very time consuming and difficult. For this purpose, the user creates separate folders and divides the files according to the years and further groups them according to the content as shown in figure below:-
Fig: File Organized in specific folders
Similarly, in java, one can organize the files using packages. A package is a namespace that groups related classes and interfaces and organizes them as a unit. Conceptually, one can think of packages as being similar to different folders created on a computer to store files. For example, one can keep source files in one folder, images in another, and executables in yet another folder. Software written in java is composed of several classes. Therefore, it is advisable to keep the classes organized by placing related classes and interface into packages.
Packages have the following features:
- A package can have sub packages.
- A package cannot have two members with the same name.
- If a class or interface is bundled inside a package, it must be referenced using its fully qualified name, which is the name of the java class including its package name.
- If multiple classes and interfaces are defined within a package in a single java source file, then only one of them can be public.
- Package names are written in lowercase.
- Standard packages in the java language begin with java or javax.
Advantages of using packages
- One can easily determine that these classes are related.
- One can know where to find the required type that can provide the required functions.
- The names of classes of one package would not conflict with the class names in other packages as the package creates a new namespace. For example, myPackage1. Sample and myPackag2.Sample.
- One can allow classes within one package to have unrestricted access to one another while restricting access to classes outside the package.
- Packages can also store hidden classes that can be used within the package, but are not visible or accessible outside the package.
- Packages can also have classes with data embers that are visible to others classes, but not accessible outside the package.
- When a program from a package is called for the first time, the entire package gets loaded into the memory. Due to this, subsequent calls to related subprograms of the same package do not require any further disk Input/Output (I/O).
Type of Packages
The java platform comes with a huge class library which is a set of packages. These classes can be used in application by including the packages in a class. The library is known as the application programming interface (API). The packages of the API represent the most common tasks associated with general-purpose programming. Every java application or applet has access to the core package in the API, the java.lang package.
For example, the string class stores the state and behavior related to character string; the File class allows the developer to create, delete, compare, inspect, or modify a file on the file system. Similarly, the Socket. Class allows the developer to create and use network sockets, and the various Graphical User Interface (GUI) control classes such as Button, Checkbox, and son on provide ready to use GUI controls. There are thousands of such classes to select and use. These ready to use classes allow a programmer to focus on the design of the application rather than on the infrastructure required to make it work.
The different types of java packages are as follows:
- Predefined packages
- User-defined packages
The predefined packages are part of the java API. Predefined packages that are commonly used are as follows:
- Java.io
- Java.util
- Java.awt
The user-defined packages are created by the developers. To create user-defined packages, perform the following steps:
1. Select an appropriate name for the package by using the following conventions:
- Package names are usually written in all lower case to avoid conflict with the names of classes or interfaces.
- Companies usually attach their reversed internet domain name as prefix to their package names. For example, com.sample.mypkg for a package named mypkg created by a programmer at sample.com.
- Naming conflicts occurring in the projects of a single company are handled according to the naming conventions specific to that company. This is done usually by including the region nae or the project nae after the company name. For example, com.sample.myregion.mypkg.
- Package names should not begin with java or javax as they are used for packages that are part of java API.
- In certain cases, the internet domain name may not be a valid package name. For example, if the domain name contains special characters such as hyphen, if the package name consists of a reserved java keyword such as char, or if the package name begins with a digit or some other character that is illegal to use as the beginning of a java package name. In such a case, it is advisable to use an underscore as shown in the table below:
Domain Name |
Suggested Package Name |
Sample-name.sample.org |
Org. sample. Sample_name |
Sample.int |
Int_. sample |
007name.sample.com |
com.sample._007name |
Table: Package Names
2. Create a folder with the same name as the package.
3. Place the source files in the folder created for the package.
4. Add the package statement as the first line in all source files under that package as depicted in code snippet given below. Note there can only be one package statement in a source file.
Code snippet:
Package StaticMembers {
Publicstaticvoidmain(String [] args)
{ }
}
5. save the source file SaticMembers.java in the package session.
6. Compiles the code as follow:
Javac staticMembers.java
OR
Compile the code with –d option as follows:
Java –d . StaticMembers.java
Where, -d stands for directory and ‘.’ Stands for current directory. The command will create a sub-folder named and store the compiled class file inside it.
7. From the parent folder of the source file, execute it using the fully qualified name as follows:
Java session9.staticMembers