alokowanie dla tablicy, a następnie użycie konstruktora
Person.java
<code>public class Person { public String firstName, lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFullName() { return(firstName + " " + lastName); } } </code>PersonTest.java
<code>public class PersonTest { public static void main(String[] args) { Person[] people = new Person[20]; //this line . for(int i=0; i<people.length; i++) { people[i] = new Person(NameUtils.randomFirstName(), NameUtils.randomLastName()); //this line } for(Person person: people) { System.out.println("Person's full name: " + person.getFullName()); } } } </code>
W powyższym kodzie użyliśmy dwa razy „nowego”. Czy ten kod jest poprawny lub zły? Pierwszy dotyczy alokacji tablicy. Ale dlaczego drugi? To z notatek z wykładów.