1

I am trying to add many objects to a LinkedList. The first (incorrect way) that I tried was the following:

Person class:

public class Person 
{
    public double age;
    public String name;
}

(first way) Main file:

import java.util.LinkedList;
public class Main 
{
    public static void main(String[] args) 
    {
        LinkedList<Person> myStudents = new LinkedList<Person>();
        Person currPerson = new Person();   
        for(int ix = 0; ix < 10; ix++)
        {
            currPerson.age = ix + 20;
            currPerson.name = "Bob_" + ix;
            myStudents.add(currPerson);
        }

        String tempName_1 = myStudents.get(1).name; \\This won't be "Bob_1"
        String tempName_2 = myStudents.get(2).name; \\This won't be "Bob_2"
    }
}

The second way:

import java.util.LinkedList;
public class Main 
{
    public static void main(String[] args) 
    {
        LinkedList<Person> myStudents = new LinkedList<Person>();
        for(int ix = 0; ix < 10; ix++)
        {
            Person currPerson = new Person();   
            currPerson.age = ix + 20;
            currPerson.name = "Bob_" + ix;
            myStudents.add(currPerson);
        }

        String tempName_1 = myStudents.get(1).name;  \\This will be "Bob_1"
        String tempName_2 = myStudents.get(2).name;  \\This will be "Bob_2"
    }
}

The second way works just fine, but is there a better (or more correct) way to do it? If the second way only uses the addresses of those objects, will that make it risky (as this address might be replaced at later point)?

Roronoa Zoro
  • 1,013
  • 2
  • 15
  • 25
  • 2
    Yes as you've seen, you cannot pass java object by value - only by reference. If you truly want to only pass by value, the best solution might be to call clone() on the object when inserting it into your list (assuming your call to clone succeeds) – ControlAltDel Apr 23 '12 at 00:38
  • @user1291492, does that make the second way also incorrect. I mean what if the addresses of those objects were used by another variable at later times? – Roronoa Zoro Apr 23 '12 at 00:41
  • 2nd way will work. After adding currPerson to the list, calling currPerson = ...; won't change what's in the list. However if you call currPerson.age = -1 after inserting into the list, that will still change the object in the list – ControlAltDel Apr 23 '12 at 00:43
  • @user1291492, There will be 10 different currPersons at different addresses in the memory. Are you saying that those addresses won't be used by any other variables? – Roronoa Zoro Apr 23 '12 at 00:46
  • 1
    The second way is the best way to do it, and will work at least as well as any other solution. – Louis Wasserman Apr 23 '12 at 00:47
  • 1
    Suggested reading: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – David J. Liszewski Apr 23 '12 at 01:04
  • There is no such thing as 'object ... by value' in Java, short of Serialization at least. – user207421 Apr 23 '12 at 01:29

1 Answers1

-1

The second way is correct, although one could argue that you could do:

import java.util.LinkedList;
public class Main 
{
    public static void main(String[] args) 
    {
        LinkedList<Person> myStudents = new LinkedList<Person>();
        Person currPerson = null; 
        for(int ix = 0; ix < 10; ix++)
        {
            currPerson = new Person();   
            currPerson.age = ix + 20;
            currPerson.name = "Bob_" + ix;
            myStudents.add(currPerson);
        }

        String tempName_1 = myStudents.get(1).name;  \\This will be "Bob_1"
        String tempName_2 = myStudents.get(2).name;  \\This will be "Bob_2"
    }
}

And declare currPerson outside the loop. This should "save" a little bit of space, but it's an infinitely small optimization. The JVM will probably optimize that little bit for you anyway.

At any rate -- as you've seen, the second way is the way to go.

debracey
  • 6,517
  • 1
  • 30
  • 56
  • it does not matter where you declare a variable. when compiled, these are all just spaces in a stack frame. Declaring a variable in a loop etc is useful in static checking – ControlAltDel Apr 23 '12 at 01:01