☰ See All Chapters |
Packages in Java
Java Packages are used for organizing set of related classes and interfaces. In java every class, interface belongs to a package. Packages provide a namespace for the classes within the package. So we can have different classes with same name present in different packages. For example, in the J2SE,
The classes that are used to perform input and output are in the java.io and java.nio packages.
The fundamental classes of the Java language are in the java.lang package.
The classes used for networking and sockets are in the java.net package.
The classes used for creating GUI applications are in the java.awt, javax.swing, and other packages.
java & javax are two main packages which are provided by Java. There are about 136 packages (sub packages) in the J2SE, depending on the version of J2SE.
Syntax of Java Package
Packages are created with the keyword called package. Package declaration should be the first statement in a java file. Package declaration starts from package keyword followed by the name of the package. The standard coding practice for giving a package name is lower case. If you do not explicitly declare a class in a package, the class belongs to the default package. The package statement should be the first line in the source file. Two package declaration statements are not allowed in the same source file. Below is the syntax to declare package.
package packagename;
Java Package Example
As stated earlier, the standard coding practice for giving a package name is lower case. If there are more words in the package name then word should be separated by dot (.) character.
package demo;
package demo.test;
package com.java4coding;
package com.java4coding;
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } |
Creating java file “HelloWorld.java” with the package com.java4coding is the creation of java file “HelloWorld.java” inside the directory (folder) com/java4coding. Java file “HelloWorld.java” resides inside directory java4coding. Directory java4coding resides inside directory com. If you want to create package com.java4coding, you should create the folder structure com/java4coding, now inside this you should create java files.
Creating package from eclipse
Creating package from eclipse is easy, Right click on src, select New > Other
Now search for package and select Java > package
Enter the package name and click Finish, we entered com.java4coding.test for package name.
Now you can see the package com.java4coding.test created inside src folder. Here in eclipse you cannot see the folder structure com/java4coding/test, but when navigate to project through windows explorer you can see folders, test folder inside java4coding, java4coding folder inside com folder.
Uses of package
A package is used for organizing set of related classes and interfaces.
Packages help in organizing the files of your projects in a better way.
Packages are used to avoid naming conflicts between two classes (when both classes have same name).
Packages help in reusability of class.
Packages provide security to class and its members.
Packages tell the history of class or packages help us to find origin of class.
Application Programming Interface
The Java Standard Library (or API) includes hundreds of classes and methods grouped into several functional packages. Most commonly used packages are:
Language Support Package (java.lang): A collection of classes and methods required for implementing basic features of Java.
Utilities Package (java.util): A collection of classes to provide utility functions such as date and time functions.
Input/Output Package (java.io): A collection of classes required for input/output manipulation.
Networking Package (java.net): A collection of classes for communicating with other computers via Internet
AWT Package (java.awt): The Abstract Window Tool Kit package contains classes that implements platform-independent graphical user interface.
Applet Package (java.applet): This includes a set of classes that allows us to create Java applets. The use of these library classes will become evident when we start developing Java programs.
All Chapters