how to initiate an array of objects java code example

Example 1: java initialize object array

import java.util.stream.Stream;

class Example {
  
  public static void main(String[] args) {
    int len = 5;  // For example.
    
    // Use Stream to initialize array.
    Foo[] arr = Stream.generate(() -> new Foo(1))  // Lambda can be anything that returns Foo. 
                      .limit(len)
                      .toArray(Foo[]::new);
  }
  
  // For example.
  class Foo {
    public int bar;

    public Foo(int bar) {
      this.bar = bar;
    }
  }
}

Example 2: how to make array of objects in java and use it

//create class
class enemies {
   int marks;
}
//create object array
enemies[] enemiesArray = new enemies[7];
//assign value to object
enemiesArray[5] = new enemies(95);