Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Let's write a Ticket class

Unique numbers for Ticket objects


nextTicketNumber as an instance variable

nextTicketNumber as a class variable

Summary of Ticket so far

class Ticket {

    private String occasion;
    private String date;
    private int ticketNumber;
    private static int nextTicketNumber = 1;
    public Ticket(String occasion, String date) {
        this.occasion = occasion;
        this.date = date;
        ticketNumber = nextTicketNumber;
        nextTicketNumber = nextTicketNumber + 1;
    public String toString() {
        // not yet written

Instance variables versus class variables

They populate memory in different ways

Class variables

The final modifier

The usefulness of named constants

Public named constants