Access Control and JAVA Packages - 080204
Access Control and JAVA Packages - 080204
JAVA Packages
Introduction
What is Package?
A package is a namespace that organizes a set of
related classes and interfaces.
Or
•A Java package is a mechanism for organizing Java
classes into namespaces.
• Default package:
If a package declaration is omitted, all classes in that
directory are said to belong to the "default" package.
Defining a Package
• package Package_Name;
Must be the first statement in a class or interface definition.
Example:
package P1;
public class Test
{
public static void main(String arr[])
{
System.out.println(“This is package P1”);
}
}
Compilation:
\Package \P1> javac Test.java
Execution:
\Package> java P1.Test
import package_name.*;
Built-in Packages
The Java API is a library of pre written classes, that are free to use, included in the Java
Development Environment.
Note : The library contains components for managing input, database programming, and much
much more.
https://docs.oracle.com/javase/8/docs/api/
To use a class or a package from the library, you need to use the import keyword:
Import a Class
If you find a class you want to use, for example, the Scanner class, which is used to get user
input.
In the example above, java.util is a package, while Scanner is a class of the java.util
package.
Note : To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation.
For example, we will use the nextLine() method, which is used to read a complete line.
User-defined Packages
To create your own package, you need to understand that Java uses a file system directory
to store them, just like folders on your computer.
Private No No No No