×

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

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 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.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 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 Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.