DEV Community

Cover image for Introduction to Java Packages and Access Modifiers
Sharique Siddiqui
Sharique Siddiqui

Posted on • Edited on

Introduction to Java Packages and Access Modifiers

Understanding how to organize and protect your code is vital to writing robust Java programs. That’s where packages and access modifiers/specifiers come into play. With these tools, you’ll ensure your classes are neatly arranged and sensitive parts of your code are accessible only where you want.

What Is a Java Package?

A package in Java is essentially a folder that contains related classes, interfaces, and sub-packages. Packages help organize your code, prevent naming conflicts, and control access to classes.

Why use packages?

  • Organize code into logical groups (e.g., utils, models, controllers)
  • Avoid naming clashes if two classes have the same name in different packages
  • Manage access between classes

How to declare a package:

At the very top of your Java file, use:

java
package mypackage;
Enter fullscreen mode Exit fullscreen mode

Now, to use a class from another package, you import it:

java
import mypackage.MyClass;
Enter fullscreen mode Exit fullscreen mode

Example:
Suppose you have the following structure:

text
project/
    utils/
        MathHelper.java  // package utils;
    main/
        App.java         // package main;
Enter fullscreen mode Exit fullscreen mode

In App.java, you can import MathHelper:

java
import utils.MathHelper;
Enter fullscreen mode Exit fullscreen mode

The Default Package

If you don't explicitly declare a package, your class belongs to the default package (no package statement). However, using explicit packages is best practice—even for small projects—as it simplifies scaling to larger, more complex codebases.

Java Access Modifiers: Who Can See or Use What?

Access modifiers determine the visibility of classes, fields, methods, and constructors. They let you protect data and enforce encapsulation, a core principle of object-oriented programming.

1. public

  • Visible everywhere: any class from any package can access.
  • Use for: code intended for broad use (e.g., main entry points, APIs).
java
public class MyClass { ... }
Enter fullscreen mode Exit fullscreen mode

2. private

  • Visible only inside the same class.
  • Use for: sensitive data and helper methods you don’t want accessed outside the class.
java
public class Person {
    private String name;         // Only accessible inside Person
    private void setName(String n) { ... }
}
Enter fullscreen mode Exit fullscreen mode

3. protected

  • Visible in the same package and subclasses (even in other packages).
  • Use for: fields/methods intended for extension, but not public API.
java
protected int age;
Enter fullscreen mode Exit fullscreen mode

4. Default (no modifier, aka “package-private”)

  • Visible only within the same package.
  • Use for: internal implementation classes, details not meant for outside users.
java
class InternalHelper { ... }  // No modifier = package-private
Enter fullscreen mode Exit fullscreen mode

Quick Access Table

Modifier Same Class Same Package Subclass (diff pkg) Everywhere
public
protected
default (*)
private

*No modifier means package-private.

Best Practices

  • Use private fields; provide public getters/setters if needed.
  • Favor public for API classes/methods that users need.
  • Use packages to keep related code together and avoid confusion.
  • Don’t overexpose: The fewer things you make public, the safer and more maintainable your code will be.

Real-World Example

java
package shapes;

public class Circle {
    private double radius;           // only Circle can access
    public Circle(double r) {
        this.radius = r;
    }
    public double getArea() {
        return Math.PI * radius * radius;
    }
}
Enter fullscreen mode Exit fullscreen mode

Another class in the shapes package can use Circle because it’s public, but it cannot access the radius field directly because it’s private.

Final Thoughts

Mastering packages and access modifiers transforms your Java code from a simple script into a scalable, professional application. Organize your files with packages, protect your data with the right access level, and you’ll be ready for real-world Java development!

Check out the YouTube Playlist for great java developer content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud

Top comments (0)