They describe other behaviour of a member or class.
| Modifier | Applies to | Meaning |
|---|---|---|
static | attributes, methods, nested classes | Belongs to the class, not to objects; shared by all |
final | attributes, methods, classes | Attribute: constant. Method: cannot be overridden. Class: cannot be inherited |
abstract | classes, methods | Class: cannot be instantiated. Method: no body, subclasses must supply one (see 3.8) |
synchronized | methods | Only one thread may run it at a time |
transient, volatile | attributes | Used in serialization and multithreading (advanced) |
Static example: counting objects
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()); // 2A static method cannot use this or instance attributes directly, because it does not belong to any object.
Final examples
final double PI = 3.14159; // constant
final class Utility { } // cannot be extended
class A { final void show() { } } // cannot be overriddenStatic in more depth
Static attributes are created once, when the class is first loaded, and are shared by every object. They are used for:
- values shared by all objects, such as a counter or the next ID number;
- constants, together with
final:static final double VAT_RATE = 0.18;.
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:
class MathUtil {
static int square(int x) { return x * x; }
}
int r = MathUtil.square(7); // 49, no object neededA 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
- Final variable: it can be given a value only once. A final attribute must be set where it is declared or in every constructor, and the compiler checks this. In the example above,
idis final and is set in the constructor, so it can never change afterwards. - Final method: subclasses cannot override it. Use this when a method's behaviour must stay the same in the whole family, for example a security check.
- Final class: no class can extend it.
Stringis a final class, so nobody can create a modified String that behaves differently.
A final reference does not make the object constant
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 objectfinal 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
staticmeans one copy for the class; call static methods on the class name.- A static method cannot use
thisor instance attributes directly. finalmeans it cannot change: a constant value, a method that cannot be overridden, or a class that cannot be extended.