Java | Functions | Question 1

Output of following Java program?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Main {
    public static void main(String args[]) {   
             System.out.println(fun());
    
   
    int fun()
    {
      return 20;
    }
}

chevron_right


(A) 20
(B) compiler error
(C) 0
(D) garbage balue


Answer: (B)

Explanation: main() is a static method and fun() is a non-static method in class Main.

Like C++, in Java calling a non-static function inside a static function is not allowed


Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.