×

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

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.