- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Organize Java Code with Classes, Packages, and Access Modifiers
In this Code Lab, you will learn how to structure a Java application using packages, control access to your classes and their members with access modifiers, and compile and run a multi-file, multi-package application from the command line. You will build a simple library management system from the ground up, reinforcing core Java principles of encapsulation and modularity.
Lab Info
Table of Contents
-
Challenge
Step 1: Creating the Data Model
Every application needs a way to represent its data. In your library app, the most fundamental piece of data is a book. You will create a
Bookclass to model this.To keep your code organized, you'll place this class inside a
modelpackage. The standard convention in Java is to use a reverse domain name for package names (e.g.,com.mycompany.project.model). This ensures uniqueness and provides a clear structure. You will usecom.library.model.Need Help? If you get stuck, reference the solution files in the
solutiondirectory. Each step has its own subdirectory (step1,step2,step3,step4) with complete solution files numbered sequentially (e.g.,Book-1-1.java,Book-1-2.java) to show progression within each step. Next, you'll apply the principle of encapsulation. This means you'll hide the internal state (the fields) of yourBookobject from the outside world. You do this by declaring the fields asprivate. To allow controlled access, you'll providepublicmethods (getters) to read the data. This prevents other parts of your code from directly and perhaps incorrectly modifying the state of aBookobject. --- This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us. -
Challenge
Step 2: Implementing the Repository Layer
Now that you have a
Bookmodel, you need a way to store and retrieve them. This is the job of the Repository Layer. A repository acts as a collection of domain objects, abstracting away the details of how they are stored (in your case, a simple list; in a real app, it could be a database).You'll create a
BookRepositoryclass in its ownrepositorypackage (com.library.repository) and use animportstatement to make theBookclass available. Here, you'll explore different access modifiers. You want the method that retrieves all books (findAll) to be available to any other class in the application, so you'll make itpublic. However, the method that initializes your sample data (init) is an internal helper. It should only be accessible to classes within the same package. For this, you usedefaultaccess (achieved by not writing any access modifier). This prevents classes from outside therepositorypackage from accidentally re-initializing your data. -
Challenge
Step 3: Building the Service Layer
The Service Layer sits between the application's entry point and the repository. Its role is to contain the business logic. For example, if you wanted to find only books by a certain author, the logic for that would go in the service layer.
Your
LibraryServicewill use theBookRepositoryto perform its tasks. It will hide the repository from theMainapplication class, providing a cleaner separation of concerns. You'll create it in aservicepackage (com.library.service). The service class exposes the high-level operations your application can perform. By creating apublicmethodlistAvailableBooks(), you provide a simple, stable API for any client (like yourMainclass) to use, without needing to know any details about how or where the books are stored. -
Challenge
Step 4: Assembling the Application
Finally, you need an entry point to start your application. The
Mainclass, containing thepublic static void main(String[] args)method, serves this purpose. It will be in thecom.librarypackage.This class is the 'client' of your service layer. It will create a
LibraryServiceinstance and use it to fetch and display the books, demonstrating how all the pieces you've built fit together. -
Challenge
Step 5: Compiling and Running
With all your classes written, the final step is to compile and run them. When working with packages, you can't just run
javac *.java. You need to use specific flags to tell the compiler about your project structure.javac -d <output_dir>: The-dflag specifies the directory where the compiled.classfiles should be placed. The compiler will automatically create the package directory structure inside this output directory.-sourcepath <source_dir>: This tells the compiler where to find the.javasource files.
You will compile all your files into a
bindirectory. To run the compiled code, you use thejavacommand. You need to tell the Java Virtual Machine (JVM) where to find your compiled classes using the classpath flag.java -cp <classpath>orjava -classpath <classpath>: This flag sets the path where the JVM will look for classes. Since you compiled everything into thebindirectory, that will be your classpath.com.library.Main: After the classpath, you provide the fully qualified name of the class that contains themainmethod you want to execute. ### Congratulations! You've successfully built a structured Java library application! These core concepts are essential for professional Java development.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.