Java Packages
If you look carefully at the java library then you will find that classes have been organized in different categories according to their category. If you want to use those classes, then you have to import the packages. Why did this happen? Classes could also be accessed even directly.With the help of Packages in java, you can easily maintain classes and interfaces in java. With the help of Packages, you can maintain different categories of classes in different packages. By doing so, the productivity of the programmer increases.
Java packages provide access protection to your classes and interfaces. If you have not used any access modifier while creating a class in the package, then classes can not access classes of other packages. Because the package private modifier is applied to that class.
The problem of duplicate names is solved from Java packages.
Creating Java Packages
You use the package keyword to create the package. Next to this keyword, you write the unique name of the package.Example1: Create java package
package myPackage;
This should be the first line of your file. After that, all the classes and interfaces you create will be included in that package. If your classes which you want to add to a package are all in different files then everyone needs to be stored in a file. All you need to do is write this statement on top of all the files, then all those classes will be included in the same package.
Some Common Java Packages
Java has a main package called Java API package. In this package, there are other packages for your different work, which you use by importing when you create a program. Such as IO package and AWT package etc. Let's try to learn about some of the common and most commonly used packages.java.language
This package contains language support classes. These are the classes that java compiler uses itself. Therefore these classes are very important.
java.util
It provides package language utility classes. Such as vector, hashtable etc.
java.io
This pacakge provides input ouput support classes. It provides the facility for input and output of data.
java.AWT
This package contains classes to implement the graphical user interface.
java.net
This package provides classes for networking. There are classes to establish connection to the server.
java.applet
These packages provide classes to create and implement applets.
Java Sub Packages
When you create another package inside a package, the inside package is called a package.
Classes were told to classify so far but you can also classify packages with the help of sub packages.
Prevents Name Clashesh
You have ever thought that at least one eCommerce software will have at least one of the lines of source code. It can be in millions of lines. Can a programmer write so much lines or make such a big software ?? It can be made but it will take a lot of time. It is therefore seen that one of the software is created by many programmers.Now imagine that when all the programmers' code is executed simultaneously, then it is possible that some 2 programmers have given the name of class or interface as same. If this happens then the program error will show in such situation. To avoid this problem java provides a mechanism that is called package.
Every programmer places its entire code in a separate package. By doing so, if any programmers keep classes or the names of interfaces as well, then your program does not get an error.
Before using the packages, you should understand how they work. The way you create the method and variable in a class, and if there is a name similar to that of another class, then there is no error in the program. Due to being in different packages in the same way, classes with the same name do not generate any errors.