Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised October 21, 2025)

Previous: 8. Lists of objects

Animal

//
// Animal.java
//


// This is a simple class that we will use in the next example.

public class Animal
{
    public Animal(String name, String type, int age, String color)
    {
        this.name = name;
        this.type = type;
        this.age = age;
        this.color = color;
    }

    // public accessor functions
    public String getName() { return name; }
    public String getType() {return type;}
    public int getAge() {return age;}
    public String getColor() {return color;}

    // private variables
    private String name;
    private String type;
    private int age;
    private String color;


    public static void main(String[] args)
    {
        Animal gadget = new Animal("Gadget", "cat", 5, "black and white");

        System.out.println(gadget.getName() + " is a " +
                           gadget.getAge() + " year old " +
                           gadget.getColor() + " " +
                           gadget.getType() + ".");
    }
}

Output:

Gadget is a 5 year old black and white cat.

Next: