How to declare a static String array in Java


Following program shows how to declare a static string array.

Example

public class Tester {
   private static String[] array;
   static {
      array = new String[2];
      array[0] = "Hello";
      array[1] = "World";
   }
   public static void main(String[] args) {
      System.out.println("Array: ");
      for(int i = 0; i < array.length; i++){
      System.out.print(array[i] + " ");
      }
   }
}

Output

Array:
Hello World

Updated on: 24-Feb-2020

930 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements