static initilization block in java

Solutions on MaxInterview for static initilization block in java by the best coders in the world

showing results for - "static initilization block in java"
Benjamin
03 Jul 2016
1public class Demo {
2   static int[] numArray = new int[10];
3   static {
4      System.out.println("Running static initialization block.");
5      for (int i = 0; i < numArray.length; i++) {
6         numArray[i] = (int) (100.0 * Math.random());
7      }
8   }
9   void printArray() {
10      System.out.println("The initialized values are:");
11      for (int i = 0; i < numArray.length; i++) {
12         System.out.print(numArray[i] + " ");
13      }
14      System.out.println();
15   }
16   public static void main(String[] args) {
17      Demo obj1 = new Demo();
18      System.out.println("For obj1:");
19      obj1.printArray();
20      Demo obj2 = new Demo();
21      System.out.println("\nFor obj2:");
22      obj2.printArray();
23   }
24}