Plain Old Java Objects (POJO)

Data Types and Exceptions in Java

Jim White

Java Developer

Data types and exception handling overview

  • Chapter 1
    • Plain old (or ordinary) Java objects
    • Wrapper classes
    • Using packages
  • Chapter 2
    • Collections Framework
  • Chapter 3
    • Exceptions and Exception handling

How does Java deal with data, problems and issues? That is the topic of this course.

1 Photo by Mika Baumeister on Unsplash
Data Types and Exceptions in Java

Java objects as data structures

  • Java objects store and organize data.
    • Created from custom classes
  • Store data in fields
    • Act as application data "suitcases"
  • Called POJOs
    • Plain ordinary Java object
    • Or Plain old Java object
  • Simple Java objects hold and move data
    • Conform to some rules
    • Don't have any logic

Java objects, or POJOs, act as suitcases to hold and move data around an application.

1 Photo by Caroline Selfors on Unsplash
Data Types and Exceptions in Java

Getters and setters

  • Getters and Setters are public
    • Fields are private
  • Getter and Setters protect the data in a POJO
    • Promote data encapsulation
  • Hide the implementation details of underlying fields
Data Types and Exceptions in Java

POJO class guidelines

  • POJO class should:
    • Be public
    • Have public getters/setters for all fields
      • Fields should be private
    • Have a default (no argument) constructor

POJO classes should follow these rules

1 Photo by Anastasiya Badun on Unsplash
Data Types and Exceptions in Java

POJO don'ts

  • POJO class should not:
    • Be tied to a framework
    • Extend other classes
    • Implement any interface
      • Other than Serializable in some cases
    • Contain business logic
  • Even these rules are sometimes relaxed
    • Try to keep POJOs simple

POJO classes should not have any of these traits

1 Photo by Anastasiya Badun on Unsplash
Data Types and Exceptions in Java

Getters

  • Getter method names
    • Start with "get" (boolean getters start with "is")
    • End with the field name
    • Lower camel case
    • Example: get + make = getMake
  • Take no parameters
  • Return the field value
  • Getters can hide details about field implementation
    private String make;

    public String getMake() {
        return make;
    }
    private short on;

    public boolean isOn() {
      if (on = 0) {
        return false;
      } else {
        return true;
      }
    }
Data Types and Exceptions in Java

Setters

  • Setter method names
    • Start with "set", end with the field name
    • Lower camel case
    • Example: set + make = setMake
  • Take a single parameter - new value of the field
    • Use this to distinguish field from parameter
  • Return void (nothing)
  • Can check the validity of the data
    private String make;

    public void setMake(String make) {
        this.make = make;
    }
    private int age;

    public void setAge(int age) {
      if ((age >= 0) && (age <= 120)) {
        this.age = age;
      }
    }
Data Types and Exceptions in Java

POJO example

  • Public class
  • Private fields
  • Public getters/setters
    • Getters return the value of a field
    • Setters set the value of a field
  • Default / no argument constructor
public class Car {   // POJO class is public
    private String model;  // Fields are private
    private int year;

    // Default no arg constructor

    // Public getters to access POJO data
    public String getModel() {
        return model;
    }
    public int getYear() {
        return year;
    }
    // Public setters to set POJO fields
    public void setModel(String model) {
        this.model = model;
    }
    public void setYear(int year) {
        this.year = year;
    }
}
Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...