The Best Technical Blog Ever

twitterrss

Packages in Java

Java packages are a mechanism to group java classes & interfaces that are related to each other, into the same module called package. 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. It looks like a directory in a file system. A package can contain subpackages.


Advantages of using a package:

  • In java, For larger applications, we split the code into multiple java classes and the classes with similar functions are placed in the same package to make them easy to locate. Since it is the easiest way to figure out which package contains which class. Packages and sub-packages are the easiest way to organize the classes.
  • It avoids naming conflicts when we use same name for classes. The names of your classes won't conflict with the class names in other packages since the package creates a new namespace and the classes are referenced through the package names. So, we can avoid naming collision.
  • Using packages, restrictions can be imposed on the access of other package classes. It gives access protection to the classes from external classes. A protected class is visible to other classes in the same package. Access specifiers work on package boundaries (between the classes of other packages).
  • The package concept helps us manage and use files stored in jar files in more efficient ways. With the help of packages, we can easily maintain classes.
  • Packages group related classes so that they can be easily distributed.

Types of package:

  • User defined package: The packages defined by programmers to group related classes/interfaces are called user-defined package.
  • Built-in package: They are standard packages which come as a part of Java Runtime Environment.

Sl.No Package Name Description
1 java.io Contains classes for supporting input / output operations.
2 java.util Contains utility classes which supports date / time operations, vectors, hash tables, random numbers, dictionary, etc.
3 java.lang Contains language support fundamental classes for primitive data types, math operations, strings, thread etc. This package is automatically imported.
4 java.applet Contains classes for creating Applets.
5 java.awt Contains classes for supporting input / output operations.Contains classes for implementing the components of graphical user interface ( like buttons, lists, windows, menus, etc. ).
6 javax.swing Contains classes for additional graphics capabilities
7 java.net Contains classes for supporting network communications.
8 java.sql contains classes and interface to perform almost all JDBC operation such as creating and executing SQL Queries.
9 javax.sql (JDBC extension API.) Contains classes and interface to access server-side data.

How to create a package:

Every class in Java belongs to a package. Classes are added to the default package when the source programs compiled. To put a class in a specific package, you should use package statement with the package name at the top of the program. It must be the first line in the source file.


package packagename; 

Assume you have a file called Circle.java, and you want to put this file in a shape package. First thing you have to do is to specify the keyword package with the name of the package you want to use (shape in our case) on top of our source file.


Example:
package


If a package statement is not used then the class/interfaces will be put into an unnamed package. A class must be defined as public in order to be accessed by other programs. Only one package declaration can appear in a source file.


Subpackage:

Subpackage: A package created inside another package is known as a subpackage. When we import a package, subpackages are not imported by default. They have to be imported explicitly.


import java.io.*;

here, "io" is a subpackage created inside java package.


Java Package Naming Conventions:

Package names are written in lower case to avoid conflict with the names of classes or interfaces. Packages in the Java language begin with java. or javax. To avoid creating packages with the same names as other public Java packages, it is recommended that you start your package hierarchy with the reverse domain name of your company.


For example, com.solsoftsys.mypackage

How to use package

There are three ways to access the public classes from outside the package.


  1. using the fully qualified class name
  2. import the the package class
  3. import the entire package

Fully qualified class name:

You can simply use the name of the class if the code you are writing is in the same package as that class or if that class has been imported. However, if you are trying to use a class from a different package and that package has not been imported, you must use the member's fully qualified name, which includes the full package path down to the subclass containing the class, and also including the class name itself. For example, the fully qualified name for JOptionPane is javax.swing.JOptionPane. You could use this qualified name to create an instance of javax.swing.JOptionPane.


javax.swing.JOptionPane ob = new javax.swing.JOptionPane();

Example:
package


If a member in one package shares its name with a member in another package and both packages are imported, you must refer each member by its qualified name. Since fully qualified class name is the only way to resolve the namespace problems. For example, the Date class exists in two packages - java.util and java.sql. Importing these two packages in a program gives ambiguity problem to the compiler as JVM is unable to judge which Date class should be given to the programmer. To solve this problem, the class name is given along with package name (i.e., fully qualified class name).


Example:
package


Here, java.util.Date is a fully qualified name. This method is the slow one and the code becomes difficult to read. As an alternative, you can import the member/ its package, then use its simple name.


importing the package class:

If class Demo and Calculation are located the same Java package and class Demo needs to use the class Calculation, you must reference class Calculation inside class Demo. The compiler will accept references between these two classes.


Example:
package


However, if class Demo and Calculation are located in different Java packages, then class Demo must import class Calculation in order to use it.


Example:
package

import org.solsoftsys.Calculation;// import only Calculation class from org.solsoftsys package import java.util.Vector; // import only Vector class from util package

If you import a class by declaring package.classname, then you can access only the declared class of that package.


Example:
package

Importing the entire package (all classes):

If you need to use a lot of classes from a certain Java package, importing them one at a time results in lot of import statements. This is resolved by importing all classes of a package using a single statement.


import java.util.*; // import all the class from util package

Here, * symbol imports all the classes and interfaces of the particular package but not subpackages.


Example:
package

The Static Import Statement:

In some situations, you need to access static final fields (constants) and static methods frequently from one or two classes. Prefixing the name of these classes over and over can result in cluttered code. The static import feature of Java 5 gives you a way to import that constants and static methods without giving the prefix name of their class. That means, it facilitates accessing of any static member of an imported class directly (i.e without using the class name). For example, the methods in the java.lang.Math class are all static, so you call them directly from the class. It defines the PI constant and many static methods, including methods for calculating sines, cosines, tangents, square roots, power value, maxima, minima, exponents, and many more. For example,


Example:
package

Example:
package

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status