Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
270 Members
Join this group to post and comment.
Information Technology
27 Jan 2019

What are java packages?

Packages are used in java in order to avoid naming conflict, to control access, for searching and ysage of classes, inheritance, enumeration etc. easier. It provides access protection and namespace management.

While creating a package you must choose a name for the package which is followed by the package statement along with the name at top of every source file that contains the classes, inheritance, enumeration that you want to include in the package.

To compile the java code with package statement you have to use the following statement 

javac -d Destination_folder

With the above line a folder with the package is created at the given destination address.

The Import keyword :

IF a class wants to use another class in the same package, the package name need not be used. For example a class A is added to the already existing class B with a package.

The Directory of packages:

There are two major result if a class is present under a package,

1)The name of the package becomes a part of the class.

2)The name of the package must match the directory structure where the corresponding bytecode resides.

Shruthi Pillai
Shruthi Pillai
Information Technology
3mos ago

Packages encapsulate a group of classes, sub packages and interfaces.  It prevent naming conflicts . It makes searching/locating and usage of classes, interfaces, enumerations and annotations easier.

AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
3mos ago

A java package is a group of similar types of classes, interfaces and sub-pakcages.Packages are used for preventing namespace conflicts.A default member (without any specifier)is accessible by classes in the same package only.Packages in java can be categorized in two form,built in package and user define package.There are many built in packages such as java,lang,set,Javax,swing,util,SQL,io.

The programmers can define their own packages to bundle group of classes/interfaces.It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations are related.Using packages it is only easy to provide access control and easier to locate the related classes. 

Advantage of Java package:

1.Java package is used to categorize the classes and interfaces so that they can be easily maintained.

2.It provided access protection and removes naming collision.