• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Java Packages

by admin

A Package can be stated that a grouping of related types (classes, interfaces, enumerations, and annotations ) providing access protection and namespace management.

Some of the existing packages in Java are:

  • java.io – classes for input , output functions are bundled in this package.
  • java.lang – bundles the fundamental classes.

Creating a Package

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

To compile the Java programs with package statements, you have to use -d option as shown below.

$ javac -d Destination_folder file_name.java

folder with the given package name is created in the specified destination, and the compiled class files will be placed in that folder.

Example:

/* File name : Human.java */
package Human;

interface Human {
   public void work();
   public void eat();
}

Now, let us implement the above interface in the same package Human.

package animals;

/* File name : MammalInt.java */

public class MammalInt implements Animal {

   public void eat() {
      System.out.println("Mammal work");
   }

   public void travel() {
      System.out.println("Mammal eat");
   } 

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}

Now compile the java files as shown below:

$ javac -d . Animal.java 
$ javac -d . MammalInt.java

You can execute the class file within the package and get the result as shown below.

Mammal work
Mammal eat

The import Keyword

If a class wants to use another class in the same package, the package name need not be used. Classes in the same package find each other without any special syntax. Example Here, a class named Oldschools is added to the payroll package that already contains Employee. The Oldschools can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Oldschool class.

package payroll;
public class Oldschools {
   public void payEmployee(Employee e) {
      e.mailCheck();
   }
}

What happens if the Employee class is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package.

The fully qualified name of the class can be used. For example:

payroll.Employee

The package can be imported using the import keyword and the wild card (*). For example:

import payroll.*;

The class itself can be imported using the import keyword. For example:

import payroll.Employee;

Set CLASSPATH System Variable

To display the current CLASSPATH variable, use the following commands in Windows and UNIX (Bourne shell):

  • In Windows → C:\> set CLASSPATH
  • In UNIX → % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use:

  • In Windows → C:\> set CLASSPATH =
  • In UNIX → % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable:

  • In Windows → set CLASSPATH = C:\users\jack\java\classes
  • In UNIX → % CLASSPATH = /home/jack/java/classes; export CLASSPATH

Filed Under: Java

Some more articles you might also be interested in …

  1. Basics of Java Operators
  2. JAVA Basic Syntax
  3. Java Field and Method Modifiers
  4. Java File and I/O
  5. Java – Documentation Comments
  6. Java – Networking
  7. Java – Arrays
  8. JAVA Installation
  9. Introduction to Java Interfaces
  10. Arrays in Java

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright