×

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work.

Packages

The package in Java is a collection of Classes and Interfaces. The packages are used to avoid naming conflicts and to reduce the complexity of writing the difficult code. The packages give the access control.

The packages in Java are classified into two types

  1. Built-in Packages / Predefined Packages
  2. User-defined packages

Built-in / Predefined packages:

The Built-in/Predefined packages are the packages which are already written by the developers of Java. These are given with the JDK.  There are various predefined/ Built-in packages, some are mentioned below:

  1. java.lang – It contains classes and interfaces that are given fundamental with java
  2. java.util – It contains the Collection Frameworks like ArrayList, LinkedList , Calendar, Date etc.
  3. java.io – It contains the input and output operations.
  4. java.awt – It contains Graphical User Interfaces used for painting,graphics and Images.
  5. java.net- It used for the networking applications.

User-defined Packages

The User defined packages are the packages that are created by the developers.

In the creation of user-defined packages, the “Package” keyword is used.

Syntax:

pack package_name;

Example:

//Example program for User-defined packages
package kotte;
public class Demo
{
public static void main(String args[])
{
System.out.println(“Demo Package”);
}
} 

Compile Java Package:

To compile the Java package we use commands like “javac -d . package_name.java”. The “-d” used to specify the path to her class file stored.

Import a Package in Java.

To import the package in Java, the “import” keyword is used.

Example:

import java.lang.*;

Access packages:

There are 3 ways of accessing packages

  1. import package_name.*;
  2. import package_name.classname;
  3. fully qualified name.

Using “import package_name.*;”

The “import package_name.*;” is used to access to all the classes and interface that are included within that package.

Example:

package kotte;
public class Demo
{
public void show()
{
System.out.println(“Welcome”);
} 
} 


// class with demo
import kotte.*;
class Pdemo
{
public static void main(String args[])
{
PDemo d = new PDemo();
d.show();
}


  
}

Output:

Welcome

In the above program we created a package “kotte” . by  using th import keyword we are importing the “kotte” package in Xdemo class.

Using “import package_name.classname;”

The “import package_name.classname;” is used to access to the specific classes and interface that are included within that package.

Example:

package kotte;
public class Demo
{
public void show()
{
System.out.println(“Welcome”);
}
public void display()
{
System.out.println(“Bye”);
} 
} 
public class ExDemo
{
public void hi()
{
System.out.println(“Greetings”);
} 
}


// class with demo
import kotte.Exdemo;
class Xdemo
{
public static void main(String args[])
{
ExDemo d = new ExDemo();
d.hi();
}  
}

Output:

Greetings

In the above program we created a package “kotte” . In that package there are two different classes . By  using the import keyword we are importing the “kotte.Exdemo” we are specifying the class that are present in that package.

Using Fully Qualified Name:

With the help of using the fully qualified name we do not need the import keyword. In this type accessing the package is “package_name.Classname” format.

Example:

package kotte;
public class KxDemo
{
public void show()
{
System.out.println(“Welcome”);
}
 
} 


// class with demo
class Xdemo
{
public static void main(String args[])
{
kotte.Demo d = new kotte.Demo();
d.show();
}


  
}

Output:

Welcome

In the above program we didn’t use any import keyword, but we have to specify the package name before the creating an object of a class like (package_name.classname).


Related Topics

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.