Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




Java - Package

No matter if you are a Windows or Mac user, you always use folders to keep your files inside it. So that all the related files are grouped together.

Same applies for java as well. To group all the related classes and Interfaces together, we use Package.

A java Package is quite similar to a folder. And as we know Classes and Interfaces are stored in a '.java' file. Similarly, the java files are stored in a Package, the same way Windows and Mac users stores their files inside a folder.

So, a java package is something where all the related classes and Interfaces are kept.


How do we create a Package in java?

A java package can be created using the package keyword and it should be the first statement in the file.

Let's say we want to keep a java file 'Human.java'(Which contains a 'Human' class) inside a package named 'myownpackage'.

So, let us understand this with the below example :



Human.java

package myownpackage;

class Human{

  int age;
  String name;

  -- getters, setters and constructors ---

}


In the above case we have a file called 'Human.java' . Inside the file we have declared the package 'myownpackage' using the package keyword.


package myownpackage;

And a folder named 'myownpackage' will be created and the java file 'Human.java' will be saved inside it.

java_package

Also let us say we have an 'Animal' class inside a java file 'Animal.java' . And we wanted to keep it inside the same package i.e. myownpackage.


Animal.java

package myownpackage;

class Animal{

   int noOfLegs;
   String type;

   -- getters, setters and constructors ---

}

Similarly, we have created a file 'Animal.java' and declared the package 'myownpackage' using the package keyword.


package myownpackage;

Now, along with the file 'Human.java', 'Animal.java' will also be stored in it.

java_package

How do we access the contents of a package from a different package?

Let us say, we have created a different package named 'mainpackage' that has the 'main()' method. And we need to use both the 'Human' and 'Animal' object in it.


Main.java

package mainpackage;
import myownpackage.*;

class Main {

   public static void main(String[] arg) {

    Human human = new Human();
    Animal animal = new Animal()
    human.setName("John");
    human.setAge(42);
    animal.setNoOfLegs(4);
    animal.setType("Dog");
    System.out.println("The name is : "+human.getName()+" and the age is : "+human.getAge());
    System.out.println("It is a : "+animal.getType()+" and has "+animal.getNoOfLegs()+" legs");
   }
}

Output :


   The name is : John and the age is : 42
   It is a : Dog and has 4 legs

The above example is quite simple. We are using the objects of Human and Animal and setting some values and displaying them.


The only catch is Human and Animal class belongs to a different package. Just have a look at the below diagram :

java_package

The Main.java is in a package called 'mainpackage' and both 'Animal.java' and 'Human.java' are in 'myownpackage'.


Using the import Keyword

Now, to use 'Animal' and 'Human' objects in the 'Main' class, we have used the 'import' statement.


import myownpackage.*;

The above statement imports all the classes available in 'myownpackage'.


Also, you can import individual classes using 'import' statement:


import myownpackage.Animal;

The above statement will allow you to use only Animal class.


Similarly, the statement:


import myownpackage.Human;

Will allow you to import only the Human class.