Programming Tips - Java: Best way to initialize an array of a class

Date: 2012sep22 Update: 2025sep27 Language: Java Keywords: init Q. Java: Best way to initialize an array of a class A. Here is a full example:
class Demo { static class Person { String first; String last; Person(final String first, final String last) { this.first = first; this.last = last; } } static Person[] people = new Person[] { new Person("John", "Smith"), new Person("Steve", "Jobs"), new Person("Bill", "Gates"), }; public static final void main(String []args) { for (Person person : people) { System.out.println("person=" + person.first + " " + person.last); } } }
Output:
person=John Smith person=Steve Jobs person=Bill Gates