How to Import a Package in Java? Full Guide

This article will discuss how to import a package in Java. Interfaces, sub-packages, and classes are grouped in Java using packages. We can import just one type from a package, or we can import every kind in a package by using an asterisk.

Java’s import statement enables you to use a package’s full contents or specific classes and interfaces specified inside. The import statement’s standard form is:


import package.name.ClassName; // to only import a specific class

import package.name.*

Continue reading to learn more about how to import a package in Java.

See also: Java Program to Crop Image Using BufferedImage Class

Packages in Java

java package

 

A package in Java is a means to group relevant interfaces, sub-packages, and classes. Among the uses for packages are:

  • Avoiding name clashes. 
  • Enabling enumerations, classes, annotations, and interfaces simpler to look for, find, and use
  • Access control at the package level is offered by protected and default, providing controlled access. A protected member may be used by classes belonging to the identical package and its subclasses. 
  • Packages can be thought of as encapsulating or hiding data.

The last step is to combine related classes into packages. Then, one can use it in your program by creating an easy import class from pre-existing packages. A package is a grouping of related classes, some of which are kept for internal usage while other aspects of which become available and exposed. As often as we require them in our program, we can reuse existing classes from the packages. You can also check out Java Command Line Tool Pop-Up: Troubleshooting And Solutions.

How to import a package in Java?

javascript objects to json

Let’s talk about how to import a package in Java right now. The “import” keyword is utilized in Java to import the package.

Example:


import java.lang.*;

Access packages:

There are three methods for obtaining packages.

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

Using “import package_name.*;”

You can access all of the classes and interfaces in that package by typing “import package_name.*;”.

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

The package “kotte” was generated in the program above. The “kotte” package is imported into the Xdemo class using the import keyword.

See also: How To Convert Celsius To Fahrenheit In Java?

Using “import package_name.classname;”

Use of the specific interfaces and classes contained in that package is made possible by the command “import package_name.classname;”.

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

We built a package called “kotte” in the program above. Two separate classes can be found in that package. The “kotte.Exdemo” class included in that package is imported using the import keyword. You must also check out Java Mobile Applications: Building Robust Apps For Modern Devices.

Using a fully qualified name:

We may eliminate the necessity for the import keyword by utilizing the fully qualified name. The format for getting to a package of this kind is “package_name.Classname”.

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

Although we did not employ the import keyword in the program above, we still needed to supply the package name before generating a class object, as in (package_name.classname).

You are now aware of how to import a package in Java.

See also: How To Download JRE For Windows?

Static java import

static method in java

A type’s static members (fields and methods) (class or interface) can be referred to using the import statements of Java 5.

As an illustration, the Math class contains several static properties like E and PI and static methods like sin() and cos(). Our code would appear as follows if we didn’t use static imports:


int degree = 38;

double tetha = Math.PI * Math.sin(degree) + 3 * Math.cos(degree);

Utilizing a static import

import static Java.lang.Math.*;

Following that, our code would seem shorter:


int degree = 38;

double theta = PI * sin(degree) + 3 * cos(degree);

How to import a class in Java?

how to import a package in java

Let’s learn how to import a class in Java after talking about how to import a package in Java. Type the code below if you locate a class you would like to utilize, such as the Scanner class, that’s used for collecting user input:

Example:


import java.util.Scanner;

Scanner is a class within Java.util package in the example above, which uses Java.util. The next line () method, which is utilized for reading a complete line, will be applied in our example.

Example

Getting user input with the Scanner class:


import java.util.Scanner;

class MyClass {

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

System.out.println("Enter username");

String userName = myObj.nextLine();

System.out.println("Username is: " + userName);

}

}

How do packages operate?

how to import a package in java

Directory structure and package names have a close relationship. For instance, a package with the name college.staff.cse would include three directories: college, staff, and cse, with staff being inside the college and cse being present in both. Additionally, the CLASSPATH variable allows access to the college directory; specifically, it contains the path to the parent directory of the college. The purpose is to ensure that classes are simple to find. you can also check out Top Java Project Ideas For Final Year Project.

Types of packages

packages in java

There are two types of packages:

Built-in Packages

Some of the commonly used built-in packages have the following features:

1) Java.lang: Consists of classes that support the language (e.g., classes that describe basic data types and mathematical operations). The import of this package is automatic.

2) Java.io: Classes for handling input and output operations can be found in this package.

3) java.util: Dictionary, Linked List, and support for Date/Time operations are just a few examples of the utility classes in Java.util.

4) java.applet: There are classes for creating applets in this package.

5) java.awt: Provides classes for creating graphical user interface elements.

6) Java.net: Classes that facilitate networking operations are available in this library.

User-defined Packages

Programmer designs their packages, which are known as user-defined packages. “Package” is a keyword that is used when creating user-defined packages.

Syntax:


pack package_name;

Example:

package kotte;

public class Demo

{

public static void main(String args[])

{

System.out.println(“Demo Package”);

}

}

FAQs

In Java, is it possible to import methods?

In Java, you don't import methods. You can use static imports more complexly, but you generally only import classes and packages. You can perform a static import when you are importing a static function.

What does a Java import statement do?

Java's import statement makes it straightforward to specify all classes available to a program under a package with only one word. It is advantageous since the programmer needs help to write the complete class definitions. As a result, it makes the program easier to read.

How to import a Java file into another?

To import a Java file into another, you must first build an instance of the variables class.

Where in Java should import statements go?

The import statements should go after the package declaration and before the class definition.

Conclusion

We hope that this post about how to import a package in Java is helpful. We saw how packages work. The types of packages are also discussed. The essential building blocks of the Java programming language are package and import.

See Also: 5 Java Automation Framework For Efficiency

Leave a Comment