Core Java - Package


Packages are used in Java inorder to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerationsss and annotations easier, etc.
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations) providing access protection and name space management.

Some of the existing packages in Java are:
  • java.lang : bundles the fundamental classes
  • java.io : classes for input , output functions are bundled in this package

  • Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related.
    Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classed.

    Creating a package :
    When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the 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.
    If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.

    Eg:
    Let us look at an example that creates a package called animals. It is common practice to use lowercased names of packages to avoid any conflicts with the names of classes, interfaces. Put an interface in the package animals:
        /* File name : Animal.java */
        package sdbt;
        interface Animal{
        public void eat();
        public void sleep();
        }
    
    
    Now, put an implementation in the same package animals:
        package sdbt;
        /* File name : MammalInt.java */
        public class Mammal implements Animal{
            public void eat(){
            System.out.println("Mammal eats");
            }
            public void sleep(){
            System.out.println("Mammal sleeps");
            }
            public int noOfLegs(){
            return 0;
            }
            public static void main(String args[]){
            Mammal m =new Mammal();
             m.eat();
             m.sleep();
            }
        }
    
    
    Output :
    Mammal eats
    Mammal sleeps


    import Keyword :
    If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax.
    Eg:
    Here, a class named Boss is added to the payroll package that already contains Employee. The Boss can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss class.
        package sdbt;
        public class Boss{
            publicvoid payEmployee(Employee e){
            e.mailCheck();
            }
        }
    
    
    What happens if Boss 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.
  • Eg:
        sdbt.Employee
    
  • The package can be imported using the import keyword and the wild card (*).
  • Eg:
        import sdbt.*;
    
    
  • The class itself can be imported using the import keyword.
  • Eg:
        import sdbt.Employee;
    
    
    Note : A class file can contain any number of import statements. The import statements must appear after the package statement and before the class declaration.

    Import statements:
    In Java if a fully qualified name, which includes the package and the class name, is given, then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class. For eg,the following line would ask compiler to load all the classes available in directory java_installation/java/io
        import java.io.*;
    
    
    Threre we are going to see the example
        import java.io.*;
        public class Employee{
            //states    
            String name;
            int age;
            String designation;
            double salary;
            //constructors 
            public Employee(String name){
            this.name = name;
            }
            //behaviours
            public void empAge(int empAge){
            age = empAge;
            }
            
            public void empDesignation(String empDesig){
            designation = empDesig;
            }
            
            public void empSalary(double empSalary){
            salary = empSalary;
            }
            /* Printing the Employee details */
            public void printEmployee(){
            System.out.println("Name: "+ name );
            System.out.println("Age : "+ age );
            System.out.println("Designation : "+ designation );
            System.out.println("Salary : "+ salary);
            }
        }
    
    
    As mentioned before, the processing starts from the main method. Therefore in-order to run this Employee class there should be main method and objects should be created. We will be creating a separate class for these tasks. Given below is the EmployeeTest class, which creates two instances of the class Employee and invokes the methods for each object to assign values for each variable. Save the following code in EmployeeTest.java file
        import java.io.*;
        public class EmployeeTest{
            public static void main(String args[]){
            Employee empOne =newEmployee("Jones");
            Employee empTwo =newEmployee("Smith");
            // Invoking methods 
            empOne.empAge(25);
            empOne.empDesignation("Senior Software Engineer");
            empOne.empSalary(1000);
            empOne.printEmployee();
            empTwo.empAge(21);
            empTwo.empDesignation("Software Engineer");
            empTwo.empSalary(500);
            empTwo.printEmployee();
            }
        }
    
    
    Ouput:
    C :> javac Employee.java
    C :> javac EmployeeTest.java
    C :> java EmployeeTest
    Name : Jones
    Age : 25
    Designation : SeniorSoftwareEngineer
    Salary : 1000.0
    Name : Smith
    Age : 21
    Designation : SoftwareEngineer

    The Directory Structure of Packages :
    Two major results occur when a class is placed in a package:
  • The name of the package becomes a part of the name of the class, as we just discussed in the previous section.
  • The name of the package must match the directory structure where the corresponding bytecode resides.

  • Here is simple way of managing your files in Java:
    Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java.
    Eg:
        // File Name : Car.java
        package vehicle;
        public class Car{
            // Class implementation.
        }
    
    
    Now, put the source file in a directory whose name reflects the name of the package to which the class belongs:
        ....\vehicle\Car.java
    
    Now, the qualified class name and pathname would be as below:
  • Class name -> vehicle.Car
  • Path name -> vehicle\Car.java (in windows)

  • In general, a company uses its reversed Internet domain name for its package names. Example: A company's Internet domain name is apple.com, then all its package names would start with com.apple. Each component of the package name corresponds to a subdirectory.
    Eg: The company had a com.apple.computers package that contained a Dell.java source file, it would be contained in a series of subdirectories like this:
        ....\com\apple\computers\Dell.java
    
    
    At the time of compilation, the compiler creates a different output file for each class, interface and enumeration defined in it. The base name of the output file is the name of the type, and its extension is.class
        /* File Name: Dell.java */
        package com.apple.computers;
        public class Dell{
            //statements
        }
        class Ups{
            //statements
        }
    
    
    Now, compile this file as follows using -d option:
        $javac -d .Dell.java
    
    
    This would put compiled files as follows:
        .\com\apple\computers\Dell.class
        .\com\apple\computers\Ups.class
    
    
    By doing this, it is possible to give the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.
    The full path to the classes directory, classes, is called the class path, and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path.
    Say classes is the class path, and the package name is com.apple.computers, then the compiler and JVM will look for .class files in classes\com\apple\compters.
    A class path may include several paths. Multiple paths should be separated by a semicolon (Windows) or colon (UNIX).
    By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in the class path.

    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


  • ☛ Join to Learn from Experts: Java Training by TesDBAcademy
    (Core Java -String)