java - Possible JDK bug in System.console() -


the following code come java.lang.system.console() method:

private static volatile console cons = null;  /**  * returns unique {@link java.io.console console} object associated  * current java virtual machine, if any.  *  * @return  system console, if any, otherwise <tt>null</tt>.  *  * @since   1.6  */  public static console console() {      if (cons == null) {          synchronized (system.class) {              cons = sun.misc.sharedsecrets.getjavaioaccess().console();          }      }      return cons;  } 

in opinion, these bug in method. should write this:

public static console console() {      if (cons == null) {          synchronized (system.class) {             if (cons == null)                  cons = sun.misc.sharedsecrets.getjavaioaccess().console();          }      }      return cons;  } 

am right? what's opinion?

if implementation of javaioaccess, find there's null-check inside it:

public console console() {     if (istty()) {         if (cons == null)             cons = new console();             return cons;         }     return null; } 

as method way initialize variable, it's not problem nested null check resides in method. it's still impossible 2 distinct console objects.


Comments