Chivid Ram wrote:In the above code, I am not able to figure out why it is throwing error on line 3. same declaration(line 3 & 4) doesn't throw any error inside method.
As mentioned in Paul's post, you are only allowed to have fields, constructors, methods, intializers (both static and instance), classes and interfaces in your class. All other code (like
a=new int[3]; in your code snippet) is not allowed and will result in a compiler error.
Here are a few alternatives to initialize that array.
You could also use a seperate method to create the array and use one of the above alternatives to initialize the array reference variable