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
Published on 06-Feb-2018 11:04:47
Advertisements