I have written a small piece of code:
System out = null;
out.out.println("Hello");
This is working fine and is printing “Hello” on the console.
Now in my program or my scope, there are two objects having the name out. One is the object of System and another is the object of PrintStream.
Why am I not getting a compiler error/ runtime error that says Duplicate local variable out.?
What am I missing here?
No, there’s just one object named
out, theSystem-type local variable. The other is namedout.out, it is not directly “in your scope”.There is no reason for this to cause a compile-time error.
(BTW, calling static methods/referencing static fields via
nullreferences isn’t really good practice, it’s pretty confusing.)