Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Java | Functions | Question 1

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Output of following Java program?




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

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


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
Last Updated : 03 Dec, 2021
Like Article
Save Article
Similar Reads