Packages with Static Imports

Java 8 min min read Updated: Mar 30, 2026 Beginner
Packages with Static Imports
Beginner Topic 4 of 25

Packages with Static Imports

As Java applications grow, the number of classes, interfaces, utilities, and reusable components also increases. If everything is kept in a single location without structure, the code becomes difficult to manage. Java solves this problem through packages.

Packages help organize related classes and interfaces into logical groups. They also avoid naming conflicts and improve code maintainability. Along with packages, Java provides an additional feature called static import, which allows direct use of static members without writing the class name repeatedly.

Key Concept: A package is used to group related Java classes and interfaces, while static import allows direct access to static members of a class without prefixing the class name each time.

What is a Package in Java?

A package in Java is a namespace used to organize related classes, interfaces, enums, and annotations. In simple words, a package acts like a folder that groups similar Java files together.

For example, in a real project:

  • student-related classes may go in com.school.student
  • teacher-related classes may go in com.school.teacher
  • utility classes may go in com.school.util

Packages make code more structured and easier to locate.

Why Packages are Important

Packages are used in Java for multiple reasons:

  • Organization: related classes stay together
  • Maintainability: large projects become easier to manage
  • Reusability: classes can be reused across modules
  • Name conflict prevention: classes with same name can exist in different packages
  • Access control: package-level access helps in encapsulation

Types of Packages in Java

Java packages are mainly of two types:

  • Predefined packages
  • User-defined packages

1. Predefined Packages

These are built-in packages provided by Java. They contain ready-made classes and interfaces.

Examples:

  • java.lang → fundamental classes like String, System, Math
  • java.util → utility classes like Scanner, ArrayList, Date
  • java.io → input-output related classes
  • java.net → networking classes
  • java.sql → database-related classes

Some packages, like java.lang, are imported automatically.

2. User-Defined Packages

These are packages created by programmers to organize their own classes.

Example:

java package com.edugators.demo; public class Message { public void show() { System.out.println("Welcome to Edugators"); } }

Here, com.edugators.demo is a user-defined package.

How to Create a Package in Java

To create a package, use the package keyword at the top of the Java file. The package declaration must be the first statement in the source file, except comments.

java package com.example.app; public class Test { public static void main(String[] args) { System.out.println("Package example"); } }

In this example:

  • package com.example.app; defines the package name
  • Test class belongs to this package

Package Naming Convention

Package names are usually written in lowercase to avoid confusion with class names. In real projects, package names often follow reverse-domain naming style.

Example:

text com.company.project.module

This style helps make package names unique across organizations.

Compiling and Running a Program with Package

When a class belongs to a package, the file must be placed in the correct folder structure.

Suppose file structure is:

text com/ └── example/ └── app/ └── Test.java

And the file contains:

java package com.example.app; public class Test { public static void main(String[] args) { System.out.println("Hello from packaged class"); } }

Compile

bash javac -d . Test.java

Run

bash java com.example.app.Test

Output

text Hello from packaged class

What is Import in Java?

When a class is placed inside another package, you need to import that class before using it, unless you use its fully qualified name.

The import keyword allows one class to use another class from a different package.

Example of Import

Suppose there is a class:

java package com.edugators.util; public class Helper { public void showMessage() { System.out.println("This is helper class"); } }

Now another class wants to use it:

java import com.edugators.util.Helper; public class Main { public static void main(String[] args) { Helper h = new Helper(); h.showMessage(); } }

Because of the import statement, the class can be used directly.

Ways to Access a Class from Another Package

There are mainly two ways:

1. Using Fully Qualified Name

java public class Main { public static void main(String[] args) { com.edugators.util.Helper h = new com.edugators.util.Helper(); h.showMessage(); } }

This works, but the code becomes lengthy.

2. Using Import Statement

java import com.edugators.util.Helper; public class Main { public static void main(String[] args) { Helper h = new Helper(); h.showMessage(); } }

This is cleaner and more readable.

Types of Import Statements

1. Import Specific Class

java import java.util.Scanner;

Only the Scanner class is imported.

2. Import All Classes from a Package

java import java.util.*;

This allows the use of all available classes in that package, but it does not import sub-packages.

Important: Importing java.util.* imports classes inside java.util, but not packages like java.util.concurrent.

What is Static Import in Java?

Static import is a feature introduced in Java 5. It allows direct access to static fields and static methods of a class without using the class name.

Normally, static members are accessed using the class name. For example:

java System.out.println(Math.sqrt(25)); System.out.println(Math.PI);

With static import, the class name can be omitted.

java import static java.lang.Math.*; public class Demo { public static void main(String[] args) { System.out.println(sqrt(25)); System.out.println(PI); } }

Here, sqrt() and PI are used directly without writing Math.

Syntax of Static Import

Import a Specific Static Member

java import static java.lang.Math.PI; import static java.lang.Math.sqrt;

Import All Static Members

java import static java.lang.Math.*;

Example Without Static Import

java public class Demo { public static void main(String[] args) { System.out.println(Math.max(10, 20)); System.out.println(Math.min(5, 2)); System.out.println(Math.PI); } }

Output

text 20 2 3.141592653589793

Example With Static Import

java import static java.lang.Math.*; public class Demo { public static void main(String[] args) { System.out.println(max(10, 20)); System.out.println(min(5, 2)); System.out.println(PI); } }

Output

text 20 2 3.141592653589793

Both programs give the same result. The only difference is that static import removes the need to write the class name repeatedly.

When Static Import is Useful

Static import is useful when a program uses static members very frequently and repeatedly. It helps reduce code length and improve readability in certain cases.

Common use cases:

  • using methods from Math class
  • using constants repeatedly
  • writing test assertions in frameworks like JUnit

Example with Multiple Math Methods

java import static java.lang.Math.*; public class CircleDemo { public static void main(String[] args) { double radius = 5; double area = PI * pow(radius, 2); double perimeter = 2 * PI * radius; System.out.println("Area: " + area); System.out.println("Perimeter: " + perimeter); } }

When Static Import Should Be Avoided

Although static import is convenient, it should not be overused. Too much static import can make the code confusing, especially if it becomes unclear which class a method belongs to.

For example:

java import static java.lang.Math.*; import static java.lang.Integer.*; public class Demo { public static void main(String[] args) { System.out.println(max(10, 20)); } }

In complex situations, multiple imported static members may create confusion or ambiguity.

Best Practice: Use static import when it genuinely improves readability, not just to make code shorter.

Difference Between Normal Import and Static Import

Feature Normal Import Static Import
Purpose Imports classes/interfaces Imports static members
Used for Class access Static variables and methods
Example import java.util.Scanner; import static java.lang.Math.PI;
Class name required? Yes, while using static members normally No, can use static member directly

Real Project Example with Package

Suppose an application has the following structure:

text com/ └── school/ ├── student/ │ └── Student.java ├── teacher/ │ └── Teacher.java └── main/ └── MainApp.java

Student.java

java package com.school.student; public class Student { public void showStudent() { System.out.println("Student details"); } }

Teacher.java

java package com.school.teacher; public class Teacher { public void showTeacher() { System.out.println("Teacher details"); } }

MainApp.java

java package com.school.main; import com.school.student.Student; import com.school.teacher.Teacher; public class MainApp { public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.showStudent(); t.showTeacher(); } }

This is how packages make real-world Java applications structured and modular.

Common Errors Related to Packages

1. Wrong Folder Structure

If the package declaration and folder structure do not match, Java compilation or execution may fail.

2. Missing Import Statement

If a class from another package is used without import and without full package name, the compiler shows an error.

3. Confusing Package Name with Class Name

Package names should usually be lowercase to avoid confusion.

4. Overusing Static Import

Excessive static import can reduce readability rather than improve it.

Common Errors Related to Static Import

Example 1: Trying Static Import with Non-Static Member

java import static java.util.Scanner.nextInt; // Invalid

Static import works only with static members, not instance methods.

Example 2: Ambiguity

If two classes expose static methods with the same name and both are imported statically, the compiler may not know which one to use.

Best Practices for Packages

  • Use meaningful and hierarchical package names
  • Keep related classes in the same package
  • Use lowercase names for packages
  • Avoid placing all classes in the default package for real projects
  • Match package declaration with folder structure

Best Practices for Static Import

  • Use static import only when it improves readability
  • Prefer explicit static import for specific members in many cases
  • Avoid importing too many static members from different classes
  • Use it carefully in team projects to keep code understandable

Interview-Oriented Points

  • A package is a namespace used to organize related classes and interfaces
  • Packages help prevent naming conflicts and improve maintainability
  • Normal import is used for classes and interfaces
  • Static import is used for static fields and methods
  • java.lang is imported automatically
  • Static import was introduced in Java 5
  • Static import should be used only when it improves code clarity

Conclusion

Packages are one of the most important organizational features in Java. They make code clean, modular, and scalable, especially in large projects. Without packages, professional Java development would become difficult to manage.

Static import is a useful feature that allows direct use of static members without writing the class name each time. It can improve readability in specific situations, but like any feature, it should be used thoughtfully.

Quick Summary: Packages organize Java classes into structured namespaces, while static import allows direct access to static members without prefixing the class name.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators