Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Object-Oriented Programming

Java Packages and Imports: Every Class Has an Address

Two classes called Order, one in shop and one in shipping: the package keeps them apart. How the package line maps to folders, plain and static imports, using both Orders in one file, and the clash when you import both.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Object-Oriented Programming

What you will learn

  • Match a package line to the folder a class sits in
  • Import one class, a whole package, or a static method
  • Use two classes with the same name by importing one and writing the other's full name
  • Explain why a class with no package can't be seen from a packaged class

Before you start

  • Java classes and objects
  • Java static: fields, methods and blocks

Say your project has two files called Order.java. One holds an id and a total; the other holds a tracking number. When you write new Order(...), which one does Java use? Packages answer that. Each class gets its own address, a bit like country, city, street and house number.

The folder path becomes the package line

The folders org, studyeasy, s03l05 and shop nest inside each other and contain Order.java. Its package line is org.studyeasy.s03l05.shop: every dot is one folder deeper. A second Order.java sits in the shipping folder with the package org.studyeasy.s03l05.shipping. org/ studyeasy/ s03l05/ shop/Order.java shipping/Order.java package org.studyeasy .s03l05.shop; package org.studyeasy .s03l05.shipping; every dot = one folder deeper
Same class name, two folders, two packages.

By convention you take your domain, studyeasy.org, and reverse it, so the first folder is org, then studyeasy. Then something more specific (here the lecture code, s03l05), then the area of the application, shop. Package names are always lowercase, and the package line must match where the file really sits.

package org.studyeasy.s03l05.shop;   // must match the folder shop/

// public: classes in other packages may use it
public class Order {
    private String id;
    private double total;

    public Order(String id, double total) {
        this.id = id;
        this.total = total;
    }

    @Override
    public String toString() {
        return "Order " + id + ": " + total;
    }
}
package org.studyeasy.s03l05.shipping;   // a different folder, a different package

// Same class name as shop.Order: the package keeps the two apart
public class Order {
    private String trackingNo;

    public Order(String trackingNo) {
        this.trackingNo = trackingNo;
    }

    @Override
    public String toString() {
        return "Parcel " + trackingNo;
    }
}

public on the class lets code in other packages use it.

Import the one you want

package org.studyeasy.s03l05;

import org.studyeasy.s03l05.shop.Order;   // the full name, once

public class Ex01_Import {
    public static void main(String[] args) {
        // Thanks to the import, the short name is enough
        Order first = new Order("A-101", 250.0);
        Order second = new Order("A-102", 99.5);
        System.out.println(first);
        System.out.println(second);
    }
}
Order A-101: 250.0
Order A-102: 99.5

The import says exactly which Order you mean. After it, the short name works. Import a path that doesn't exist, like ...s03l05.store.Order, and it won't compile.

import static for a static method

package org.studyeasy.s03l05.util;

public class PriceUtils {
    // static: call it on the class, no object needed
    public static double addTax(double amount) {
        return amount + amount * 10 / 100;      // 10% tax
    }
}
package org.studyeasy.s03l05;

import static org.studyeasy.s03l05.util.PriceUtils.addTax;

public class Ex02_StaticImport {
    public static void main(String[] args) {
        double price = 250.0;
        double withTax = addTax(price);   // no "PriceUtils." needed
        System.out.println("Price:    " + price);
        System.out.println("With tax: " + withTax);
    }
}
Price:    250.0
With tax: 275.0

import static plus the class path plus the method name lets you call addTax(price) without PriceUtils. in front. (Static methods are covered in Java static.)

Two Orders in one file

package org.studyeasy.s03l05;

import org.studyeasy.s03l05.shop.Order;

public class Ex03_NameClash {
    public static void main(String[] args) {
        Order purchase = new Order("A-103", 120.0);    // shop.Order

        // Two classes named Order: the second gets its full name
        org.studyeasy.s03l05.shipping.Order parcel =
                new org.studyeasy.s03l05.shipping.Order("TRK-889");

        System.out.println(purchase);
        System.out.println(parcel);
    }
}
Order A-103: 120.0
Parcel TRK-889

Import one, and it gets the short name. Write the other with its full name (the fully qualified name), which needs no import.

Import both instead and Java can't choose. The compiler says a type with the same simple name Order is already defined by the single-type-import. Import one, spell out the other.

Outside every package

A class with no package line, placed straight under src/main/java, sits in the "default package", outside every package folder. A class inside a package, like Checkout in s03l05, can't see it: the compiler says "cannot find symbol". Always put classes in a package.

Four ways to reach a class

You writeEffect
import org.studyeasy.s03l05.shop.Order;one class; the short name Order works
import org.studyeasy.s03l05.shop.*;every class in that package
import static ...PriceUtils.addTax;one static method, called as addTax(price)
org.studyeasy.s03l05.shipping.Order in the codethe full name; no import needed

The package is the folder; the import says which class from which folder. Packages also matter for visibility, as you'll see in access modifiers.

Keep going in Java · 27 of 79

Previous
Java static: Fields, Methods and Blocks Shared by the Whole Class
Next post
Java Access Modifiers and Encapsulation: Private Data, Public Methods
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Java Classes and Objects: One Blueprint, Many Cars

Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.

Java Constructors and the this Keyword

A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.

Java Instance Methods, Getters, Setters and toString

Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.

Java static: Fields, Methods and Blocks Shared by the Whole Class

A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.

On this page

  • The folder path becomes the package line
  • Import the one you want
  • import static for a static method
  • Two Orders in one file
  • Outside every package
  • Four ways to reach a class
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.