0

The same type of declaration for an array is showing error when its outside the main() but complies without error when in main():

public class Array {
    int arr1[];
    arr1 = new int[10]; // shows <identifier> expected error

    public static void main(String args[]){
        int arr2[];
        arr2 = new int[10];
    }
}
Jonathan
  • 18,696
  • 6
  • 62
  • 66
  • 1
    when do you think this code would be executed `arr1 = new int[10];` ?? – jlordo Aug 20 '13 at 09:06
  • For better understandings take a look on http://stackoverflow.com/questions/1200621/how-to-declare-an-array-in-java – Pradip Aug 20 '13 at 09:12
  • Read this, you can get some idea http://stackoverflow.com/questions/4568740/can-i-initialize-an-array-outside-the-method-just-like-we-initialize-a-member-fi – Pandiyan Cool Aug 20 '13 at 09:13

4 Answers4

0

That can be done in one line, like:

int arr1 = new int[10];

If you need multiple statements, you need to put the code inside a method, constructor or initializer block.

kiheru
  • 6,493
  • 21
  • 29
  • it works fine when i declare it by any other syntax for array declaration but then why does it show error doing it this way(in separate lines) – Sudhanshu Gupta Aug 20 '13 at 09:15
  • @SudhanshuGupta The class body is not executed in the sense as methods are. That's the way the language has been [defined](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.6). For the actual *reason* why it's been decided so I can only guess, but I think it is because there's no need for that: the same effect can be achieved in a cleaner, unambiguous way using constructors or initializer blocks. – kiheru Aug 20 '13 at 09:26
0

When you want to assign a value to a class variable, you should do so when defining it

private int arr1 = new int[10];

or inside a method (e.g. 'main').

public class TestClass {
   // When you want to use the variable inside a static function, arr1 should be declared as static
   private static int arr1[] = new int[10];

   public static void main(String args[]) {
      arr1 = new int[10];
   }
}

Read this for class/member variables basics.

R. Oosterholt
  • 6,445
  • 2
  • 44
  • 71
  • You're trying to access a non-static variable from a static context. It won't compile. – jlordo Aug 20 '13 at 09:14
  • right, use of arr1 inside main was just an example: you could just posted a comment to get it fixed instead of down voting... Changed the example. – R. Oosterholt Aug 20 '13 at 09:24
  • That's exactly what I did: posted a comment. Now, as the compile time error is gone, so is my downvote. – jlordo Aug 20 '13 at 10:26
0

It does not work when you declare it in separate lines, because it turns out to be a statement.Statements are allowed only in static block,methods and constructors.

This works fine because it is field declaration

int a[]= {1,2,3};
int a[]=new int[]{1,3,4,5};

You might want to access these using objects as these are non static fields.

Malwaregeek
  • 2,224
  • 3
  • 12
  • 17
0

In any Java Class the first part is the declaration part. Which is used to declare the objects.

It does not contain any code for any specified Operations, Like Assigning a value to a variable, do a mathematical operations etc.

here you are initializing the array arr1

arr1 = new int[10];

outside a function. If you really want to do an initialization you can do it Like this

int arr1 = new int[10];

which initialise the array at the time of creation. Else, you can initialise it inside any method.It need not wants to be a main function.

Its because, a function is a set of codes that needs to be executed and a class is a group of objects and associated functions.

public class Array {
    int arr1[];

    public String anyFunction(){
        arr1 = new int[10];
    }
}

Look Here formare details about class.

Dileep
  • 5,094
  • 3
  • 19
  • 37