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; }
3 Respuestas
+ 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.
+ 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.
+ 1
Thanks everyone for your valuable ans👍