Definition of a Package:
•A package is a collection of related classes.
•A package is used to create a namespace. A namespace is a collection of names.
•Packages are used to avoid naming conflicts of classes.
•Packages act as containers for classes. Means a package contains classes.
•We can declare a class with same name in different packages.
•Examples of some of the predefined packages in Java are:
java.lang
java.awt
javax.swing
java.net
java.util
Declaring a Package:
•Java provides the keyword “package” for declaring a package.
•Syntax for declaring a package is as follows:
package package-name;
•Example for declaring a package is as follows:
package mypackage;
•Package declaration must be the first statement in the program.
•Generally the package names are written in lower case.
•Java uses file system directories to maintain packages.
•If no package is declared in the program, then it will be treated as a default package which has no name.
•So, in the above example, since mypackage is a packages, we must create a folder with the name “mypackage” in the development directory.
Example:
•So, now the package “mypackage” contains three classes, Shape, Rectangle and ShapeDemo.
Accessing a Package:
•How does the JVM know where the packages are located?
•There three ways using which the JVM can locate the packages. They are:
1) By default
2) By setting CLASSPATH environment variable
3) By using the –classpath option available with the “java” command.
Importing a Package:
•After creating a package, the classes available in the package can be used in other programs.
•For using the predefined classes available in a package, the package must be imported.
•There are three ways of importing the classes available in the package. They are:
1) Importing all the classes in the package.
2) Importing only the specific class in the package.
3) Using the fully qualified name.
Multilevel Packages:
•Packages can be maintained in hierarchical manner.
•The syntax for such multilevel packages is as follows:
package pkg1.pgkg2.pkg3;
•Java maintains the packages as folders. So the above multilevel packages are maintained as:
pkg1\pkg2\pkg3
Access Control in Packages:
•A package is a collection of related classes.
•A package is used to create a namespace. A namespace is a collection of names.
•Packages are used to avoid naming conflicts of classes.
•Packages act as containers for classes. Means a package contains classes.
•We can declare a class with same name in different packages.
•Examples of some of the predefined packages in Java are:
java.lang
java.awt
javax.swing
java.net
java.util
Declaring a Package:
•Java provides the keyword “package” for declaring a package.
•Syntax for declaring a package is as follows:
package package-name;
•Example for declaring a package is as follows:
package mypackage;
•Package declaration must be the first statement in the program.
•Generally the package names are written in lower case.
•Java uses file system directories to maintain packages.
•If no package is declared in the program, then it will be treated as a default package which has no name.
•So, in the above example, since mypackage is a packages, we must create a folder with the name “mypackage” in the development directory.
Example:
•So, now the package “mypackage” contains three classes, Shape, Rectangle and ShapeDemo.
Accessing a Package:
•How does the JVM know where the packages are located?
•There three ways using which the JVM can locate the packages. They are:
1) By default
2) By setting CLASSPATH environment variable
3) By using the –classpath option available with the “java” command.
Importing a Package:
•After creating a package, the classes available in the package can be used in other programs.
•For using the predefined classes available in a package, the package must be imported.
•There are three ways of importing the classes available in the package. They are:
1) Importing all the classes in the package.
2) Importing only the specific class in the package.
3) Using the fully qualified name.
Multilevel Packages:
•Packages can be maintained in hierarchical manner.
•The syntax for such multilevel packages is as follows:
package pkg1.pgkg2.pkg3;
•Java maintains the packages as folders. So the above multilevel packages are maintained as:
pkg1\pkg2\pkg3
Access Control in Packages:
No comments:
Post a Comment