Stop 04 · Classes & ObjectsTopics · 8/8

Packages & Imports

Part of the Java Roadmap

In short

Real projects have hundreds of classes, and two of them will inevitably both be called Order. Packages group related classes into folders with unique names, and imports let you use them without typing long names every time.

Packages organise classes into namespaces that match folder structure, usually in reverse-domain style like com.example.app. import lets you use classes from other packages.

The jaadu as a postman sorts envelopes into a big wooden rack of labelled pigeonholes, each holding a neat bundle of letters.

Think of it like this

Postal addresses

There are many people named Rohan Gupta in India, but only one at "Flat 12, Sector 18, Noida, Uttar Pradesh". A package name works like that full address: com.flipkart.orders.Order and com.myshop.orders.Order can both exist. import is like saving a contact, so you can just say "Rohan" afterwards.

Watch it happen

Package names follow folders

src/main/java
com/myshop
orders
Order.java
payments
UpiPayment.java

1/4Maven and Gradle projects keep source files under src/main/java.

The big ideas

What you need to know

Reverse-domain names

Use your domain backwards, all lowercase: com.insideedge.billing. This keeps names unique worldwide.

Folders must match

package com.myshop.orders; must live in com/myshop/orders/. The compiler relies on it.

import

import java.util.List; brings one class in by short name. java.lang (String, Math) is imported automatically.

static import

import static java.lang.Math.max; lets you call max(a, b) without Math. Use it sparingly.

See it in code

Walk through the code

java
1// File: src/main/java/com/myshop/orders/Order.java2package com.myshop.orders;3 4import java.time.LocalDate;5import java.util.List;6import com.myshop.payments.UpiPayment;7 8import static java.lang.Math.round;9 10public class Order {11    private final List<String> items;12    private final LocalDate placedOn = LocalDate.now();13    private final UpiPayment payment;14 15    public Order(List<String> items, UpiPayment payment) {16        this.items = List.copyOf(items);17        this.payment = payment;18    }19 20    public long roundedTotal(double amount) {21        return round(amount);22    }23 24    public String summary() {25        return items.size() + " items on " + placedOn + " via " + payment;26    }27}

1/4The package line comes first and matches the folder path.

A class in its own package, importing from the JDK, from another package in the same project, and one static import.

Save yourself the pain

Common mistakes

Oops

Using import java.util.*; and import java.awt.*; together, then using List.

Do this instead

Both packages have a List, so the name is ambiguous. Import the exact class: import java.util.List;

Oops

Putting a class with package com.shop; in the wrong folder.

Do this instead

Keep folders and package names in sync. IntelliJ can move the file for you with Refactor > Move.

Your mission

Organise a mini project

~20 min

Quick check

Which package is imported automatically into every Java file?

Go deeper

  1. 1Split a project into model, service and util packages.
  2. 2Learn which classes in java.lang are imported automatically.
  3. 3Avoid wildcard imports in production code for clarity.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge