0

Why This code has a compile error ?

class Test{ public static void main(String args[]) System.out.println(fun()); } static int fun() { static int x=0; return ++x; }

23rd Mar 2020, 6:01 PM
samrat kundu
samrat kundu - avatar
3 Respostas
+ 2
Wrong handling of braces between methods and class... Check again... Edit: more errors, Some more capital C in Class => wrong Correct is class In main() the argument declaration has, Small s in string is wrong. Correct is (String arg[]).. //capital S static int x=0 in static method is not allowed. static data should be declared in class level.
23rd Mar 2020, 6:05 PM
Jayakrishna 🇮🇳
+ 1
You should not declare and initialize a static variable inside a method because static context is used with something which has class level scope. If you declare a static variable inside a method then it's scope is limited and it will lead to a compile time error.
23rd Mar 2020, 6:16 PM
Avinesh
Avinesh - avatar
+ 1
Thanks everyone for your valuable ans👍
23rd Mar 2020, 6:29 PM
samrat kundu
samrat kundu - avatar