What would be the result of attempting to compile and run the following program?
class Main{//from ww w .jav a 2 s . c o m static Main ref; String [] v; public static void main (String args []){ ref = new Main (); ref.m (args); } public void m (String [] args){ ref.v = args; } }
Select 1 option
Correct Option is : E
For A.
The concept here is that a non-static method (i.e. an instance method) can only be called on an instance of that class.
Whether the caller itself is a static method or not, is immaterial.
The main method is calling ref.m()
; - this means the main method is calling a non-static method on an actual instance of the class Main (referred to by 'ref').
Hence, it is valid.
It is not trying calling it directly such as m()
or this.m()
, in which case, it would have been invalid.