×

How to Import Packages in Java

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

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.