Java Array Declaration as Static Field, Local Variable, or Method Parameter



We can declare an array as a local variable or method parameter but, an array cannot be static.

Example

public class Test{
   public void sample(){
      static int[] myArray = {20, 30};
      System.out.println();
   }
   public static void main(String args[]){
      Test t = new Test();
      t.sample();
   }
}

Error

C:\Sample>javac Test.java
Test.java:3: error: illegal start of expression
   static int[] myArray = {20, 30};
   ^
1 error
Updated on: 2019-07-30T22:30:20+05:30

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements