Packages and Access Modifiers in Java
As Java projects grow, keeping every class in one folder becomes hard to manage. Packages help you group related classes, while access modifiers decide which classes, fields, constructors, and methods can be used from other parts of the program.
What is a Package?
A package is a namespace that groups related Java classes and interfaces together.
For example, Java already provides built-in packages:
java.lang- basic classes likeString,Math,Systemjava.util- utility classes likeArrayList,HashMap,Scannerjava.io- file and input/output classesjava.time- date and time classes
In real projects, packages make code easier to find, reuse, and maintain.
Creating a Package
Use the package keyword at the top of a Java file.
package com.guidedocs.school;
public class Student { public void display() { System.out.println("Student details"); }}Important rules:
- The package statement must be the first non-comment line in the file.
- Package names are usually written in lowercase.
- Folder structure should match the package name.
For the package com.guidedocs.school, the file path should look like this:
com/guidedocs/school/Student.javaImporting Classes
To use a class from another package, use the import keyword.
import java.util.ArrayList;
public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Saurabh");
System.out.println(names); }}You can also import all classes from a package:
import java.util.*;However, importing only the classes you need is usually clearer.
Fully Qualified Class Name
Instead of importing, you can use the full package path with the class name.
public class Main { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter name: "); String name = input.nextLine();
System.out.println("Hello " + name); }}This is called using a fully qualified class name. It is useful when two packages have classes with the same name.
What are Access Modifiers?
Access modifiers control visibility. They answer this question:
From where can this class, variable, constructor, or method be accessed?
Java has four main access levels:
| Modifier | Same class | Same package | Subclass in another package | Anywhere |
|---|---|---|---|---|
private | Yes | No | No | No |
| default (no keyword) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
public
The public modifier makes something accessible from anywhere.
public class Calculator { public int add(int a, int b) { return a + b; }}Use public for classes and methods that are meant to be used by other parts of your application.
private
The private modifier allows access only inside the same class.
public class BankAccount { private double balance;
public void deposit(double amount) { if (amount > 0) { balance += amount; } }
public double getBalance() { return balance; }}Here, balance cannot be changed directly from outside the class. This supports encapsulation.
protected
The protected modifier allows access:
- Inside the same class
- Inside the same package
- Inside subclasses, even if they are in another package
class Animal { protected String name;
protected void eat() { System.out.println(name + " is eating"); }}
class Dog extends Animal { public void show() { name = "Bruno"; eat(); }}Use protected when child classes need access to parent class members.
Default Access
If you do not write any access modifier, Java uses default access, also called package-private access.
class Helper { void printMessage() { System.out.println("Package helper method"); }}This class and method can be accessed only inside the same package.
Class Access Modifiers
Top-level classes can only be:
public- default (no keyword)
Example:
public class Main { public static void main(String[] args) { System.out.println("Public class"); }}A top-level class cannot be private or protected.
Best Practices
- Keep fields
privateand access them through methods when needed. - Use
publiconly for classes and methods that should be used outside the class or package. - Use packages to group related code, such as
model,service,controller, andrepository. - Avoid putting too many unrelated classes in the same package.
- Prefer clear package names like
com.company.project.feature.
Quick Summary
| Concept | Meaning |
|---|---|
| Package | Groups related classes and avoids name conflicts |
import | Allows using classes from another package |
| Fully qualified name | Uses complete package path with class name |
public | Accessible everywhere |
private | Accessible only inside the same class |
protected | Accessible in same package and subclasses |
| default | Accessible only inside the same package |
Next Steps
After understanding packages and access modifiers, continue with Constructors, this, and super to learn how Java initializes objects and connects parent-child class behavior.