How does Java assign a value to the final System.out object?

Consider the below Java code,

import java.io.File;
import java.io.PrintStream;

public class Test {

    public final static PrintStream out = null;

    public static void main(String[] args) throws Exception {
        Test.out = new PrintStream(new File("/tmp/temp.txt"));
    }

}

      

This results in a compilation error.

Test.java:9: error: cannot assign a value to final variable out
        Test.out = new PrintStream(new File("/tmp/temp.txt"));
            ^
1 error

      

The built-in class java.lang.System

also has a similar declaration as shown below:

public final static PrintStream out = null;

      

Can anyone tell me how this is initialized System.out

?

+3


source to share





All Articles