3

Person.java

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);
    }
}

PersonTest.java

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());
        }
    }
}

In above code, we used twice "new". Is this code is correct or wrong? First one is for allocation of array. But why the second one? It's from lecture notes.

Community
  • 1
  • 1
oiyio
  • 5,219
  • 4
  • 42
  • 54
  • The second "new Person" is correct, assuming you want to insert a new Person with a random first and last name into every entry of the array "people." As far as posting code, it's usually easy to copy-paste the code and for each line that isn't previewed as code you can insert four spaces. I hope this addressed your questions, I wasn't entirely sure what you meant. – Chris Apr 06 '12 at 13:52
  • (There's a help button in the editor (`?`, top right) that has examples of how to format code. Simply put, select all your code and click on the `{}` button, or hit Ctrl+K) – Mat Apr 06 '12 at 13:54
  • ok i got how it does. -> after selecting the text ,do ctrl k . thanks.. – oiyio Apr 06 '12 at 14:09

3 Answers3

10

Yes, it is correct.

The line:

Person[] people = new Person[20]

allocates the array, full of references to null while the line:

new Person(NameUtils.randomFirstName(),
                      NameUtils.randomLastName());  //this line

fills it [the array] by instantiating objects of type Person, and assigning the reference in the array.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Ok.Then "new" keyword is not only used for allocation.It can be used for calling constructors without allocation.(because allocation may be done previously) – oiyio Apr 06 '12 at 14:03
  • 1
    No, `new` handles both allocation and construction. Arrays don't have constructors, but `new Person[20]` both allocates and initializes the array. – Taymon Apr 06 '12 at 14:13
10

new Person[20] creates an array that can hold 20 references to Person objects. It does not create any actual Person objects.

new Person(...) creates a Person object.

The critical distinction to make here is that unlike in C or C++, new Person[20] does not allocate memory for 20 Person objects. The array does not contain the actual objects; it only contains references to them.

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • 1
    In Java, allocation is not handled separately from construction. `new Person[20]` allocates the memory for an array of 20 references, and then initializes it as a `Person[]`. `new Person(...)` allocates the memory for a `Person` object and then calls the `Person` constructor. – Taymon Apr 06 '12 at 14:12
2
Person[] people = new Person[20];

only allocates memory for objects Person (filled with nulls). Then you need to fill it with particular Persons (with random name and surname int this example).

zbyszek26104
  • 437
  • 1
  • 5
  • 15