Whenever we write any statement to print to console in Java program:
System.out.print or System.out.println
In above both statements we’re calling out reference variable of PrintStream object without explicitly importing java.io package, so how we’re accessing that object methods without resulting any compile time error?
The System object has references to
java.io.PrintStreamobjects embedded in it. So you don’t need to explicitly import these – the runtime can derive this information unambiguously since it was embedded at compile-time.As you’ve identified, if you used a
PrintStreamobject directly, you’d have to import that. The compilation stage doesn’t know where to find it (it could search, but that could easily give ambiguous results).Note also (in case there’s any confusion),
java.langis implicitly imported, hence you don’t require an import statement forSystem.