How to use Java packages? [duplicate]
Why do we use packages in Java? How to use them?
Packages are mostly just a way of organizing code. The JDK has thousands of classes in, and a large application has thousands more. Why would you not want to organize those into some sort of hierarchy allowing you to find the classes you're interested in easily?
Packages also participate in access control in Java (but not in .NET, interestingly) - but I'd say the main purpose is to help humans organize their code meaningfully.
It also means that occasionally you may want or need the same class name in multiple packages - where the package name effectively provides the context. Now if you're working in a single codebase for a single application, that's usually something to try to avoid - but if you've got a large codebase where many different applications use many different parts of it, that may be better than trying to have a unique name for every single class. (A typical example of this is in user interface code - just looking in the .NET libraries, there's a "Button" class in three separate namespaces, for three separate UI frameworks.)
Think of packages in java as the folders on your PC. You like to keep different types of files in different folders, just as the same you keep different types of classes and interfaces in different packages in java.
By doing so, we get some advantages and some of them are listed below-
- Grouping similar files: You keep videos in a folder like Entertainment\Videos, documents in a folder like Personal\Documents. Like this similar kind of classes and interfaces are kept in same packages in java.
- Security: Just as you can hide or set a password to protect the contents of your files in a folder you can achieve same kind of feature by declaring your class members as protected or without any access specifiers.
- Readability and Accessibility: Suppose need to find a kind of class or interface, then you can find that kind of package by its name and proceed your search. Thus you can narrow down your search area and then find and access it in an easy way.
- Usage of only required files Suppose you're just executing a simple hello world program and you don't need so many classes to be imported, in such a case all the classes available in java library will not be imported. If you don't have packages then each and every class in java library will be imported.
We define Class/ Interface in package with a java keyword package
followed by the package structure. It must be the first statement of your Class/ Interface. If you don't define a package name then that file will be placed in a default package, which is your source code folder.
package com.my.package.MyClass;
Packages are used in java preceded by a keyword import
. Levels of packages are accessed with the dot (.) operator as we do '/' or '\' in case folders on the PC.
Import a class MyClass available in a package named com\my\package:
import com.my.package.MyClass;
Import all the classes available in a package named com\my\package:
import com.my.package.*;
Import only a static field of a class, so that you can directly access it without class name:
import static com.my.package.MyClass.myStaticField;
Packages are a way to group similar classes. You do not want to put all your classes in one package, do you?
精彩评论