Package Keyword In Java

Here I am explaining Package Control keywords in Java.
 
In Package control there are only two keywords:.
  1. Import
  2. Package 
Here is the explanation.
 
Import Keyword
    
The import keyword is used to make classes and interfaces available and accessible to the current source code, without specifying fully qualified package names. For example,
  1. import java.awt.*;  
  2. import java.util.List;  
  3. import java.io.File; 
The first import statement makes all classes/interfaces under package java.awt accesible to the current program. It uses the * wildcard character to specify 'everything' under the package.

Likewise, the second and third import statements make the interface list and the class file available to the current source code. It uses exact names instead of wildcard.

The import statements must be placed on top of the source file, only after the package statement.

Package Keyword
 
The package keyword is used to specify a directory structure to which the current source file must belong. For example,
  1. package com.mycompany.code;  
  2.    
  3. public class MyClass {  

In the above example, the source file MyClass.java must be placed under the directory /com/mycompany/code

Here are some rules regarding the package statement:

  • A source file can be put under a package or not.
  • The package statement (if specified) must be the first line in the source file.
  • Package name must follow the pattern: level1.leve2.level3....leveln

    Up Next
      Ebook Download
      View all
      Learn
      View all