Package

Package in Java:
  • Organise related classes, interfaces and sub-packages
  • It is folder structure where source and class file will be created when you use package in your class, interface and abstract class
  • Syntax to create package: package organisation or company or country .(dot) organisation Name or company Name or country Name .(dot) Module Name .(dot) sub-module
    e.g.
    package com.java.pkg;
  • Syntax to import a class: If one class is present in different package and need to use in the current class then we can use import statement.
    import java.util.Scanner;
  • Syntax to import multiple classes: If multiple classes are present in different package and need to use in the current class then we can use import statement with * (asterisk).
    import java.util.*;
  • Syntax to import static: It is used to include all static members that class into the current class.
    import java.lang.Math.*;

Example 1: Write a program that explains import static?
package com.java.pkg;

import static java.lang.Math.*;
import static java.lang.System.*;

public class StaticImport {
    public static void main(String[] args) {
        out.println(sqrt(49));
        out.println("-----------------------------------------");
        
        out.println(random());
        out.println("-----------------------------------------");
        
        out.println(max(24.5, 24.6));
        out.println("-----------------------------------------");
        
        out.println(min(40, 30));
    }
    public static int min(int a, int b) {
        System.out.println("min method of current class StaticImport");
        return a < b ? a : b;
    }
    public static double sqrt(double num){
        System.out.println("sqrt method of current class StaticImport");
        double res = num / 2, temp;
        
        do {
            temp = res;
            res = 0.5 * (temp + num / temp);
        }while( res != temp);    
        return res;
    }
}

Output:
sqrt method of current class StaticImport
7.0
-------------------------------------------------
0.9984401372419331
-------------------------------------------------
24.6
-------------------------------------------------
min method of current class StaticImport
30

Example 2: Write a program to explain package. Create a class Employee in package com.java.emp with id, name and salary as data members and Test class in package com.java.test and include the Employee class in Test class using import statement?

Employee.java
package com.java.emp;

public class Employee {
    public int id;
    public String name;
    public double salary;
        
    public Employee() {
        id = 111;
        name = "John";
        salary = 15000.0;
    }

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    
    public void display() {
        System.out.println("Id: " + id +", Name: "  + name + " and Salary: " + salary);
    }
}

Test.java
package com.java.test;

import com.java.emp.Employee;

public class Test {
    public static void main(String[] args) {
        Employee e1 = new Employee();
        e1.display();
        
        System.out.println("-------------------------------------------");
        Employee e2 = new Employee(222, "Anirudh", 10000.0);
        e2.display();
        
        System.out.println("-------------------------------------------");
        Employee e3 = new Employee(333, "Guru", 20000.0);
        e3.display();
        
        System.out.println("-------------------------------------------");
        Employee e4 = new Employee(444, "Basavraj", 25000.0);
        e4.display();
        
        System.out.println("-------------------------------------------");
        Employee e5 = new Employee(555, "Visnu", 30000.0);
        e5.display();
    }
}

Compile and run from Command Line:
javac -d . Employee.java Test.java
java com.java.test.Test

Output:
Id: 111, Name: John and Salary: 15000.0
------------------------------------------------------
Id: 222, Name: Anirudh and Salary: 10000.0
------------------------------------------------------
Id: 333, Name: Guru and Salary: 20000.0
------------------------------------------------------
Id: 444, Name: Basavraj and Salary: 25000.0
------------------------------------------------------
Id: 555, Name: Visnu and Salary: 30000.0

No comments:

Post a Comment