0
About System.in
System.in variable is public, static and final. now I've read that "System.in" is an object of type "InputSteam". Since System.in is final, then what is the value assigned to it. obviously, it can't be an InputStream object since it's an abstract class.
2 ответов
+ 1
This is right out of the System.java class.
/**
* The "standard" input stream. This stream is already
* open and ready to supply input data. Typically this stream
* corresponds to keyboard input or another input source specified by
* the host environment or user.
*/
public final static InputStream in = null;
/**
* Reassigns the "standard" input stream.
*
* <p>First, if there is a security manager, its <code>checkPermission</code>
* method is called with a <code>RuntimePermission("setIO")</code> permission
* to see if it's ok to reassign the "standard" input stream.
* <p>
*
* @param in the new standard input stream.
*
* @throws SecurityException
* if a security manager exists and its
* <code>checkPermission</code> method doesn't allow
* reassigning of the standard input stream.
*
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*
* @since JDK1.1
*/
public static void setIn(InputStream in) {
checkIO();
setIn0(in);
}
private static native void setIn0(InputStream in);
https://stackoverflow.com/questions/5951464/java-final-system-out-system-in-and-system-err
Certain classes within the Java language are handled by the compiler/JVM differently than you might expect based on the way we can build/declare class objects. These things are handled "behind the scenes".
Keep in mind that InputStream is a reference type even though it is abstract and cannot be instantiated it can be used to refer to an object that inherits from it. This means that even though it is set to null initially that it can still be set to an object that inherits from InputStream, but its reference cannot change.
At runtime the setIn() method is called and the InputStream is passed into the method and set.
Realistically you shouldn't worry too much about how System.in works behind the scenes and just keep learning.
+ 1
@ChaoticDawg Thanks, that was really helpful. I Know it doesn't really matter how it works. But I just can't let any thing pass without understanding it.