public static void main(String[] arg){ static int x=20; System.out.println("Value of x :"+ x); } } Why does the above code not compile
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
My best guess is that you cannot declare your "x" to be a static in the main method. Possible reason... static variables are class variables and I don't think they can be declared within methods (including main()). Do correct me if I am wrong. Shyam [This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
You are right. Inside an instance method a static variable makes no sense. Wihtin a static one would be redundant (I guess)