final
and static
final
and static
keywords.
final
Modifierfinal
modifier keyword makes that the
programmer cannot change the value anymore.
The actual meaning depends on whether
it is applied to a class, a variable, or a method.
We look at these three cases in more detail.
final
Classesfinal
class cannot have subclasses.
An example:
This skeleton defines a class calledpublic final class MathConstants { ... }
MathConstants
that is
publicly accessible but cannot be subclassed.
final
Variablesfinal
variable cannot be changed once it is initialized,
In the above class of mathematical constants you can for example define a
numerical approximation of pi by
It is a convention, but not obligatory, to capitalize the name of a final object.publicfinal
static double PI = 3.141592654;
final
Methodsfinal
method cannot be overridden by subclasses.
There are two reasons for introducing such a method:
final
method to generate a random constant.
publicfinal
static randomNumber() { ... }
static
Modifierstatic
keyword in the declaration.
A class variable will instantiate only one copy of the variable for the
whole class instead of a separate copy for each instance of a class.
A class variable belongs to a class, not to an instance
of the class.
You can refer to a class variable either through an instance of the class (like
a normal instance variable) or through the full class name of the form
classname.variable
.
A class method
can be referred to through an instance of the class (like a normal method)
or through the full class name of the form
classname.method
. In fact, a class is an object of the special class Class
,
so this naming system is consistent.
You can look at the examples above for how to use class variables and class methods. You can also look at the example worked out below.
Shape
class with the
subclasses Circle
and Square
.
In this example you can practice your knowledge of modifiers and
access specifiers.
public final class MathConstants { public final static double PI = 3.141592654; // constant pi }
public final class ShapesCounter { private static int shapesCount = 0; // total number of geometrical objects private static final int maxCounter = 1000; // maximum number of objects protected static int shapesCount() { return shapesCount; } protected static void incrementShapesCount() { shapesCount++; } protected static void decrementShapesCount() { shapesCount--; } }
public class Shape { protected double x, y; // position of geometrical object }
public class Circle extends Shape { protected double r; // radius private static double maxSize = 100; // maximal radius Circle(double x, double y, double r) { super.x = x; super.y = y; this.r = r; ShapesCounter.incrementShapesCount(); } public static void setMaxSize(double size) { maxSize = size; } public double area() { return MathConstants.PI * r * r; } public double circumference() { return 2 * MathConstants.PI * r; } }
public class Square extends Shape { protected double size; // radius private static double maxSize = 100; // maximal size Square(double x, double y, double size) { super.x = x; super.y = y; this.size = size; ShapesCounter.incrementShapesCount(); } public static void setMaxSize(double size) { maxSize = size; } public double area() { return size * size; } public double circumference() { return 4 * size; } }