How to declare a static String array in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Updated on 24-Feb-2020 10:43:45

Advertisements