0

below there is a for loop code.I found out by calling a custom display function that the aBook arrayList object only adding last class object thrice. Why is that happening?

Scanner s = new Scanner(System.in);
    ArrayList<LiFiAddressBook> aBook = new ArrayList<LiFiAddressBook>();
    // taking input for every LifIAddressBook and adding them to the ArrayList.
    for (int i = 0; i < 3; i++) {
        System.out.println("Entry " + i+1);
        System.out.print("Please Enter First Name: ");
        String a = s.nextLine();
        System.out.println();
        System.out.print("Please Enter Last Name: ");
        String b = s.nextLine();
        System.out.println();
        System.out.print("Please Enter Street Address: ");
        String c = s.nextLine();
        System.out.println();
        System.out.print("Please Enter City: ");
        String d = s.nextLine();
        System.out.println();
        System.out.print("Please Enter Zip Code: ");
        int e = s.nextInt();
      // in the next line we need to fire a blank scan function in order consume the nextLine. because after executing s.nextInt compiler skip a scan function for a weird reason
        s.nextLine();
        System.out.println();

        LiFiAddressBook x = new LiFiAddressBook(a, b, c, d, e);
        aBook.add(x);


    }

here is my LiFiAddressBook Class

public class LiFiAddressBook {

static  String  first_name, last_name, street_address, city_state;
static int zip_code;

public LiFiAddressBook(String first, String last, String street, String city, int zip) {
  //constructor for class object.
    first_name = first;
    last_name = last;
    street_address = street;
    city_state = city;
    zip_code = zip;
}

public  String get_first() {
    return first_name;
}

public String get_last() {
    return last_name;
}

public String get_address() {
    return street_address;
}

public String get_city() {
    return city_state;
}

public String get_zip() {
    return Integer.toString(zip_code);
}

public static void display() {
    System.out.println("First Name: "+first_name);
    System.out.println("Last Name: "+last_name);
    System.out.println("Street Address"+street_address);
    System.out.println("City State: "+city_state);
    System.out.println("Zip Code: "+zip_code);


}

}

7
  • 3
    Given the code, that can't be happening. Can you show the code where you are displaying the elements of list. Commented Aug 30, 2013 at 16:43
  • How are you verifying that its the same object thrice? Commented Aug 30, 2013 at 16:45
  • 7
    I suspect that the problem is within LiFiAddressBook. Possibly, every time you create a new LiFiAddressBook, it stomps on the values of previously instantiated objects (maybe the member fields are declared static or something?). Commented Aug 30, 2013 at 16:45
  • @ajb was right! Good guess ;) Commented Aug 30, 2013 at 16:50
  • 1
    remove the static qualifier from the the declaration of the fields! Commented Aug 30, 2013 at 16:53

3 Answers 3

2

Because of the static keyword, every time the constructor
public LiFiAddressBook(String , String , String , String , int )
is called the old values are over written by the new values and when printed the elements in list the variables of the objects of LiFiAddressBook class points to same objects. hence printing similar objects.

To be clear, actually there are 3 instances of LiFiAddressBook . but the variables/properties of those LiFiAddressBook instances references to same objects.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the static keyword. In essence, that keyword ensures that there is only one instance of those variables.

Comments

1

Make:

static  String  first_name, last_name, street_address, city_state;
static int zip_code;

Into:

String  first_name, last_name, street_address, city_state;
int zip_code;

Also you probably need to change this:

public static void display() {

To:

public void display() {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.