×

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

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

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

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

What is String in Java?

What is 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 Java...

4 minutes read.