CodesJava

Easy learning with example program codes

Package in java


Package:

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

Advantages/Benefits of package in java:

  1. Package provides unique namespace.
  2. Package provides access protection.
  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 CodesJava
 */
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. Using import keyword.
a. Import packagename.*;
All classes and interface will be accessible but not of subpackages.
b. Import packagename.classname;
Only specific class is accessible.
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.codesjava.display;
/**
 * This class is used to display entered text.
 * @author CodesJava
 */
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 CodesJava
 */
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.codesjava.display;
/**
 * This class is used to display entered text.
 * @author CodesJava
 */
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 CodesJava
 */
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 CodesJava
 */
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 CodesJava
 */
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.codesjava.display;
/**
 * This class is used to display entered text.
 * @author CodesJava
 */
public class Display {
       public void displayText(String text){
              System.out.println(text);
       }
}

Test.java

package com.codesjava.test;
import com.codesjava.display.*;
/**
 * This class is used to show sub package using import.
 * @author CodesJava
 */
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.codesjava.display;
/**
 * This class is used to display entered text.
 * @author CodesJava
 */
public class Display {
       public void displayText(String text){
              System.out.println(text);
       }
}

Test.java

package com.codesjava.test;
import com.codesjava.*;
/**
 * This class is used to show sub package classes
 * will not import if package is imported.
 * @author CodesJava
 */
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.codesjava.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.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki