Java Reference
In-Depth Information
The compiler error generated is
Square.java:4: modifier abstract not allowed here
public abstract Square(int s) {
^
1 error
A constructor cannot be abstract because it cannot be overridden. You can specify that
a method cannot be overridden as well using the
final
modifi er, discussed in the next
section.
The
final
Modifier
The
fi nal modifi er
is applied to local variables, fi elds, methods, or classes. The properties of
the
final
modifi er are
A
final
variable or field cannot be changed. These are referred to as
constants
.
A
final
method cannot be overridden. (We discussed
final
methods in Chapter 2.)
A
final
class cannot be subclassed.
A
final
variable or fi eld cannot be changed once it is assigned. The following
MyLogger
class contains a
final
fi eld, a
final
parameter, and a
final
local variable:
1. import java.io.File;
2.
3. public final class MyLogger {
4. private final File DEST;
5.
6. public MyLogger(File d) {
7. DEST = d;
8. }
9.
10. public void logMessage(final String MESSAGE) {
11. final long TIME = new java.util.Date().getTime();
12. //write time and message to file...
13. }
14. }
The fi eld
DEST
on line 4 and the parameter
MESSAGE
on line 10 are referred to as
blank
fi nals
. They are constants that do not have an initial value, and once they are assigned a
value, they cannot be changed. The
TIME
variable on line 11 is assigned the current time
and cannot be changed.
Search WWH ::
Custom Search