Keyword: static

CSCI 1933 – Introduction to Algorithms and Data Structures
Adriana Picoral

Warm-up

Identify the parts (instance variables, constructor, getters and setters, operators, helpers) in the code below:

public class Calculator {
    private double left;
    private double right;

    public Calculator(double left, double right) {
        this.left = left;
        this.right = right;
    }

    public double getLeft() {
        return left;
    }

    public double getRight() {
        return right;
    }

    public void setLeft(double number) {
        left = number;
    }

    public void setRight(double number) {
        right = number;
    }

    public double add() {
        return left + right;
    }

    public double subtract() {
        return left - right;
    }

    public double multiply() {
        return left * right;
    }

    public double divide() {
        return left / right;
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator(10, 5.6);
        System.out.println(calc.add());
        System.out.println(calc.subtract());
        System.out.println(calc.multiply());
        System.out.println(calc.divide());
    }
}

Static

Use the keyword static for methods or variables that belong to the class itself rather than instances/individual objects

  • Static methods are called without creating an object

Instance Variables vs Class Variables (use static) - Instance variables are unique to each object, while class variables are shared across all instances of a class.

  • When a class variable is changed, that attribute is changed for the class (change is realized in all objects of that class)

Static vs. non-static

  • static – property of the Class
    • Used for utility functions
    • Used for constants
    • Used for “global” like variables (shared by all instances)
  • non-static – property of instances of the class
    • used for behavior of object
    • used for instance data
    • a reasonable default

Static Methods

We can have static methods in any class, here’s an example – we will add this static method to our Calculator class:

public static boolean isOdd(int number) {
  return number % 2 != 0;
}

Here’s how we use it (note the difference between calling an object method and a static class method):

Calculator.isOdd(10)

Class Variables

When we add the keyword static to a variable in a class, that becomes a class variable (as opposed to instance variable). Let’s add some code to the main method in our Calculator class, two create two objects:

Calculator calc = new Calculator(10, 5.6);
Calculator anotherCalc = new Calculator(4, 2);
System.out.println(calc.add());
System.out.println(anotherCalc.add());
anotherCalc.setLeft(10);
System.out.println(calc.add());
System.out.println(anotherCalc.add());

Run the code and notice the output. The .setLeft(10) method call only changed the output for anotherCalc object. Change the left variable to static – how do the results change?

Looking Back

  • Give an example of when to use a static attribute
  • Challenge: Give an example of a static method other than main()