Lesson Objectives

  • Define class variables and class methods using static
  • Explain how class variables are shared by all objects of a class, and what class methods can/cannot access
  • Use a class name and dot operator to access public class variables/methods
  • Explain what the final keyword does

Instance vs. Class Variables

Recall that an instance variable belongs to an individual object, so let’s say we have:

public class Student {
    private String name;
    private int grade;
}

and we define 3 students

Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();

Each student gets their own name and grade.

Class variables are different in that they belong to the class itself:

public class Student {
    private String name;
    private int grade;

    public static int numberOfStudents = 0;
}

Now, each student s1, s2, s3, shares one copy of numberOfStudents.

For example, we could make a class:

public class Student {
    private String name;

    public static int numberOfStudents = 0;

    public Student(String n) {
        name = n;
        numberOfStudents++;
    }
}

and create a few objects:

Student a = new Student("Alice");
Student b = new Student("Bob");
Student c = new Student("Charlie");

which makes Student.numberOfStudents 3.

Static

We use static to denote that a variable or method belongs to the class itself rather than to an individual object.

This would be an instance variable:

private String name;

and this would be a class variable:

private static int numberOfStudents;

Public and Private

If we want some class variable to be accessible outside of the class, we can declare it as public:

public class Game {
    public static int players = 0;
}

which means we can do:

System.out.println(Game.players); // get # of players
Game.players = 5; // modify # of players

Class Methods

Similarly, we can define a method as static to denote it belongs to a class:

public class MathHelper {

    public static int doubleValue(int x) {
        return x * 2;
    }
}

Now we can call it without creating a MathHelper object:

int result = MathHelper.doubleValue(5);

System.out.println(result); // 10

Class methods can access class variables, other class methods, its parameters, and local variables, but NOT instance variables:

public class Counter {

    private static int count = 0;

    public static void increase() {
        count++;
    }

    public static int getCount() {
        return count;
    }

    public static void reset() {
        count = 0;
    }

    public static void resetAndIncrease() {
        reset();
        increase();
    }
}

We CANNOT do this

public class Student {
    private String name;

    public static void printName() {
        System.out.println(name); // ERROR
    }
}

final Variables

We define a variable as final if we want it to be immutable.

public static final int DAYS_IN_WEEK = 7;

Homework

In this assignment, you’ll be modifying a partiall implemented Java class called GameTracker. The bottom half of the code snippet runs debugging tests as you progress with the TODOs.

Code Runner Challenge

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Rubric

score = 0.55 + [(0.9 - 0.55)/11 * x]

where x is the number of tests passed in the homework.

Submission

  1. Create a new notebook in your portfolio homework area: _notebooks/homework.
  2. Add one raw cell at the top with the frontmatter:
    ---
    layout: post
    codemirror: true
    title: Class Variables and Methods HW
    categories: [Java]
    lesson_language: Java
    lesson_topic: Class-Variables-and-Methods HW
    lesson_part: interactive
    lesson_type: lesson
    permalink: /csa/unit_03/3_7-hw
    author: <yourGithubID>
    ---
    

Add a code cell with the GameTracker code snippet (or just run it directly by importing it as a .java file), then submit the notebook/Java file with these details:

Lesson: CSA 3.7 Class Methods and Variables
Homework: tests passed (x/11)