What are packages in Java?

Java 8Object Oriented ProgrammingProgramming

Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

  • java.lang − bundles the fundamental classes.

  • java.io − classes for input, output functions are bundled in this package.

Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, and annotations are related.

Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classes.

Executing a package

You need to compile the file with packages using –d option as −

C:\Sample>javac -d . PackageExample.java

After compiling, you can see the class files of the class created in the location com/mypackage/tutorialspoint.

PackageExample
raja
Published on 09-Feb-2018 11:17:50
Advertisements