In Java, you use the keyword final to denote a constant.
final double PI=3.14;
The keyword final indicates that you can assign to the variable once, then its value is set
once and for all.
public final class System
{
. . .
public final static PrintStream out = nullPrintStream();
. . .
}
Since out has been declared as final, you cannot reassign another print stream to it:
out = new PrintStream(. . .); // ERROR--out is final
If you look at the System class, you will notice a method setOut that lets you set System.out to a different stream. You may wonder how that method can change the value of a final variable. However, the setOut method is a static method which calls native method, not implemented in the Java programming language.
public static void setOut(PrintStream out) {
checkIO();
setOut0(out); //This is a native method
}
private static native void setOut0(PrintStream out);
final double PI=3.14;
The keyword final indicates that you can assign to the variable once, then its value is set
once and for all.
Important on final keyword:
System.out is a final variable . It is declared in the System class aspublic final class System
{
. . .
public final static PrintStream out = nullPrintStream();
. . .
}
Since out has been declared as final, you cannot reassign another print stream to it:
out = new PrintStream(. . .); // ERROR--out is final
If you look at the System class, you will notice a method setOut that lets you set System.out to a different stream. You may wonder how that method can change the value of a final variable. However, the setOut method is a static method which calls native method, not implemented in the Java programming language.
public static void setOut(PrintStream out) {
checkIO();
setOut0(out); //This is a native method
}
private static native void setOut0(PrintStream out);
0 comments:
Post a Comment