Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
2. Object-Oriented Programming
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

19. Packages & Access Modifiers

Organizing your files into folders and locking down security.

Feb 22, 20264 views0 likes0 fires
18px

[!NOTE] As your application grows from 5 files to 5,000 files, throwing everything into a single folder becomes chaos. Java uses a strict "Package" system mapped directly to your operating system's folders.

Packages (Folders)

A package is simply a directory structure. To avoid naming colllisions globally (where two companies both create a DatabaseConnection class), Java conventionally uses reversed internet domain names as the root package structure.

If your company domain is google.com, your project structure might look like this:

src/
└── com/
    └── google/
        └── auth/
            └── LoginManager.java
        └── database/
            └── QueryExecutor.java

At the absolute very top line of LoginManager.java, you MUST declare the package it lives inside:

// Line 1: Package declaration
package com.google.auth;

// Now we import classes from OTHER packages
import java.util.Scanner;
import com.google.database.QueryExecutor;

public class LoginManager { ... }

Access Modifiers Matrix

We briefly touched on public and private. But there are actually 4 distinct levels of visibility in Java. Understanding this matrix is critical for interview questions.

Modifier Access inside same Class Access inside same Package Access from a Subclass (Child) Access from the World everywhere
public ✅ ✅ ✅ ✅
protected ✅ ✅ ✅ ❌
(default) ✅ ✅ ❌ ❌
private ✅ ❌ ❌ ❌

The "Default" (Package-Private) Mystery

If you write a variable without ANY keyword (e.g., String token;), it defaults to Package-Private. Any other class living in the exact same folder (package) can see and modify that variable. But if a class in a different folder tries to access it, the compiler throws an error.

When to use what?

  • Use private for 99% of your instance variables to enforce Encapsulation.
  • Use public for classes, constructors, and methods you want the whole application to use (like a .saveData() method).
  • Use protected for variables that you want to hide from developers, but allow inheriting Child classes to manipulate directly for performance logic.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Modifiers
2 questions5 min

Continue Learning

20. Enums (Enumerations)

Intermediate
7 min

21. Exceptions: Handling Runtime Errors

Intermediate
12 min

22. The 'throw' and 'throws' keywords

Intermediate
10 min
Lesson 9 of 10 in 2. Object-Oriented Programming
Previous in 2. Object-Oriented Programming
18. Interfaces: The Ultimate Contract
Next in 2. Object-Oriented Programming
20. Enums (Enumerations)
← Back to Java Programming: From Zero to Enterprise
Back to Java Programming: From Zero to EnterpriseAll Categories