• August 26, 2014
  • Core Java
  • Comments Off on Package in java

Package in java

Package:

Package is a namespace that is used to group logically related classes and interfaces.

Advantages/Benefits of :

  1. 1. Package provides unique namespace.
  2. 2. Package provides access protection.
  3. 3. Package provides grouping of logically related classes and interfaces so easy to maintain.

How to create a package:

A package is created with package keyword.

Example:

PackageExample1.java

package test;
/**
 * This program is used to show simple use of package.
 * @author javawithease
 */
public class PackageExample1 {
        public static void main(String args[]){
               System.out.println("This is first package example.");
        }
}

Output:

This is first package example.

Download this example.

 How to access package from outside the package:

  1. 1. Using import keyword.
  2. a. Import packagename.*;

All classes and interface will be accessible but not of subpackages.

  1. b. Import packagename.classname;

Only specific class is accessible.

  1. 2. Using full qualified name.

Specific class will be accessible and no need to import but everywhere it is used fully qualified name is needed.

Example of import packagename.*

Display.java

package com.javawithease.display;
/**
 * This class is used to display entered text.
 * @author javawithease
 */
public class Display {
        public void displayText(String text){
               System.out.println(text);
        }
}

Test.java

package test;
import display.*;
/**
 * This class is used to show package use with import packagename.*.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
               Display display = new Display();
               display.displayText("Hello java.");
        }
}

Output:

Hello java.

Download this example.

Example of import packagename.classname

Display.java

package com.javawithease.display;
/**
 * This class is used to display entered text.
 * @author javawithease
 */
public class Display {
       public void displayText(String text){
              System.out.println(text);
       }
}

Test.java

package test;
import display.Display;
/**
 * This class is used to show package using import packagename.className.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
              Display display = new Display();
              display.displayText("Hello java.");
       }
}

Output:

Hello java.

Download this example.

Example of using full qualified name

Display.java

package display;
/**
 * This class is used to display entered text.
 * @author javawithease
 */
public class Display {
       public void displayText(String text){
              System.out.println(text);
       }
}

Test.java

package test;
/**
 * This class is used to show package using full qualified name.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
              display.Display display =
                             new display.Display();
              display.displayText("Hello java.");
       }
}

Output:

Hello java.

Download this example.

Subpackage:

A package inside a package is known as subpackage.

Example:

Display.java

package com.javawithease.display;
/**
 * This class is used to display entered text.
 * @author javawithease
 */
public class Display {
       public void displayText(String text){
              System.out.println(text);
       }
}

Test.java

package com.javawithease.test;
import com.javawithease.display.*;
/**
 * This class is used to show sub package using import.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
              Display display =
                       new Display();
              display.displayText("Hello java.");
       }
}

Output:

Hello java.

Download this example.

 Note: If a package is import, all classes and interface will be accessible of that package but not of subpackages. So subpackage also be import to access classes and interfaces of subpackage.

 Example:

Display.java

package com.javawithease.display;
/**
 * This class is used to display entered text.
 * @author javawithease
 */
public class Display {
       public void displayText(String text){
              System.out.println(text);
       }
}

Test.java

package com.javawithease.test;
import com.javawithease.*;
/**
 * This class is used to show sub package classes
 * will not import if package is imported.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
               //Error
               Display display = new Display();
               display.displayText("Hello java.");
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problems:
Display cannot be resolved to a type
Display cannot be resolved to a type
at com.javawithease.test.Test.main(Test.java:13)

Download this example.
 
Next Topic: Access modifier in java with example.
Previous Topic: Constructor in java with example.

Related Topics:

Package in java with example.
Access modifier in java with example.
Static import in java with example.
Package class in java with example.
this in java with example.
Instance initialize/ Anonymous block in java with example.
super in java with example.
Static in java with example.
final in java with example.

 


Comments are closed.