posted 23 years ago
Dear Sona,
You must have checked output. This program will give output :
true is a boolean Operator
This can be explained as below.
First static initialiser block is executed at time of loading the class, so it prints true.
(b'cos "Java".startsWith("") is true)
Then in main() method, processorA() method of class Q1 is called, which is static method.
The statement ("java").startsWith("null") will be evaluated to false, b'cos here "null" is string
and which is not same as ""
So, else part will be executed and null will be returned.
Point is even if null is returned, how processorB() is executed and NullPointerException is not thrown.
This is because processorB() is static method. For executing static method, the object reference is not requied.
It can be exaplained with simple example,
Q1 q1 = null;
q1.processorB(); // This is correct statement
I think this will make things clear.
Regards,
Sujit