Open In App
Related Articles

Java | Functions | Question 1

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Dec, 2021
Like Article
Save Article
Previous
Next
Similar Reads