Get startedGet started for free

Plain Old Java Objects (POJO)

1. Plain Old Java Objects (POJO)

Welcome to this course on Java data types and exception handling. I've spent three decades building Java applications and written a book on Java. I even started an open-source project using Java. Together, we take the next step in your Java journey.

2. Data types and exception handling overview

We'll learn how Java handles data and errors. First, we'll look at Plain Old Java Objects (POJOs)-simple classes that store and manage data. Then, we'll see how Java converts basic values (like numbers and letters) into objects using wrapper classes. Finally, we'll explore packages, which are built-in tools that help organize and use Java code more efficiently. Chapter 2 explores the Collections Framework - a set of data structures for handling data. Finally, Chapter 3 examines Java's exception-handling tools to effectively manage and respond to problems and issues.

3. Java objects as data structures

You already know that Java lets you create objects from custom classes, and these objects help store and organize data. These objects hold data in their fields and act as suitcases to move data around an application. Now, when these classes are kept simple, Java refers to them as Plain Ordinary (or Old) Java Objects - POJOs for short. "Simple" here means that the classes should not inherit from other classes or contain anything beyond basic logic for managing their data.

4. Getters and setters

Before getting into POJOs, let's remember getters/setters in Java. They are key ingredients of `POJOs` and help to protect data by controlling how data can be accessed and updated. Getters allow access to data safely, while setters ensure only valid values are stored. They also make POJOs more flexible, allowing field data types to change in the future without breaking the rest of the program.

5. POJO class guidelines

`POJO` classes follow guidelines so that they are easy to create, use, and understand. They should be public, have public getter/setter methods while their fields are private. They should only have the default, no argument constructor (provided by Java by default).

6. POJO don'ts

Conversely, `POJO` classes are plain. They don't extend another class. They don't implement any interface. POJO classes typically do not contain business logic. These rules are guidelines. No Java police checks our classes so rules are sometimes relaxed. In general, we abide by these rules to make our `POJOs` more easily used and understood.

7. Getters

Getter methods start with "get" (or "is" for booleans) and end with the field name - in lower camel case. They take no parameters and always return the field value. Getters provide encapsulation, hiding how data is stored and managed. For example, getters hide the fact that a boolean on/off indicator is actually a short under the covers.

8. Setters

Setter method names start with "set" before the field name. They take a single parameter - the value you wish to store in the field, and they return no value. Setter methods can check that the value being assigned is within certain parameters. As an example, limiting age to values between 0 and 120.

9. POJO example

Here is an example `POJO` class. The class is public but the fields are private. Public methods are used to get data and put data into the fields. These are called getters and setters. By default, every Java class has a no-argument constructor. `POJOs` don't have any other constructors.

10. Let's practice!

Let's try defining and using a `POJO`.