Unit 3 · OOP Concepts

3.3.2Non-access modifiers

They describe other behaviour of a member or class.

ModifierApplies toMeaning
staticattributes, methods, nested classesBelongs to the class, not to objects; shared by all
finalattributes, methods, classesAttribute: constant. Method: cannot be overridden. Class: cannot be inherited
abstractclasses, methodsClass: cannot be instantiated. Method: no body, subclasses must supply one (see 3.8)
synchronizedmethodsOnly one thread may run it at a time
transient, volatileattributesUsed in serialization and multithreading (advanced)

Static example: counting objects

Java
class Student {
    static int count = 0;          // one copy shared by all objects
    final int id;                  // set once, never changed
    String name;

    Student(String name) {
        this.name = name;
        count++;
        this.id = count;
    }

    static int getCount() {        // called on the class
        return count;
    }
}

new Student("Nimal");
new Student("Kamala");
System.out.println(Student.getCount());   // 2
class Studentstatic count = 2Student object 1id1name"Nimal"Student object 2id2name"Kamala"sharedsharedStudent.getCount() is called on the class, not on an object
A static attribute has one copy shared by the whole class; instance attributes are separate for each object

A static method cannot use this or instance attributes directly, because it does not belong to any object.

Final examples

Java
final double PI = 3.14159;       // constant
final class Utility { }          // cannot be extended
class A { final void show() { } }  // cannot be overridden

Static in more depth

Static attributes are created once, when the class is first loaded, and are shared by every object. They are used for:

Static methods belong to the class, so you call them on the class name without creating an object: Math.sqrt(25), Integer.parseInt("42"), Student.getCount(). They are the right choice for "utility" work that needs only its parameters, not an object's state:

Java
class MathUtil {
    static int square(int x) { return x * x; }
}

int r = MathUtil.square(7);   // 49, no object needed

A static method cannot use instance attributes or this directly, because when it is called there may be no object at all. It can use other static members, and it can use an object's members if it receives that object as a parameter.

Overusing static: making everything static so you never have to create objects turns a Java program back into a procedural one, with global data that anything can change. Use static only for things that really belong to the class as a whole.

Final in more depth

A final reference does not make the object constant

Java
final Student s = new Student("Nimal");
s.name = "Kamal";             // allowed: the object can still change
// s = new Student("Sunil");  // ERROR: s cannot point to a different object

final fixes the reference (which object s points to), not the contents of the object.

Naming constants

By convention, a static final constant is written in capitals with underscores: MAX_SPEED, PASS_MARK, VAT_RATE. A named constant is clearer than a "magic number". if (marks >= PASS_MARK) explains itself, while if (marks >= 50) does not. If the pass mark changes, you only change one line.

Remember

  • static means one copy for the class; call static methods on the class name.
  • A static method cannot use this or instance attributes directly.
  • final means it cannot change: a constant value, a method that cannot be overridden, or a class that cannot be extended.