why did this hashmap always returned the same object?

Discussion in 'Java' started by ravi mannan, Nov 23, 2003.

  1. ravi mannan

    ravi mannan Guest

    when I execute AccountManager, it's supposed to return an object
    in the hashmap that corresponds to the key "1", which is the object
    w/ the name "Moe Howard", instead it always returns the last object
    put into the HashMap.
    this is the output:
    cp.getName:Larry Fine
    cp.getPin:33
    cp.getAcctNum:3
    cp.getMaidenName:M

    after a long time debugging, I finally got rid of the "static" for the
    data members
    in the CustomerProfile class, and got the correct output.
    cp.getName:Moe Howard
    cp.getPin:11
    cp.getAcctNum:1
    cp.getMaidenName:F

    My big question is WHY??? Why does static do this?
    thanks in advance!
    /***************************/
    package beans;

    import java.util.*;

    public class AccountManager {
    private HashMap accounts = new HashMap();

    public static void main(String[] args){

    AccountManager am = new AccountManager();
    am.getCustomerProfiles();
    CustomerProfile cp = am.getCustomerProfile(1);
    System.out.println(
    " cp.getName:" + cp.getName());
    System.out.println(
    " cp.getPin:" + cp.getPin());
    System.out.println(
    " cp.getAcctNum:" + cp.getAcctNum());
    System.out.println(" cp.getMaidenName:"
    + cp.getMaidenName());

    }
    public void getCustomerProfiles() {
    CustomerProfile c = new CustomerProfile();

    c.setName("Moe Howard");
    c.setMaidenName("F");
    c.setAcctNum(1);
    c.setPin(11);
    accounts.put(new Integer(c.getAcctNum()), c);

    CustomerProfile d = new CustomerProfile();
    d.setName("Curly Howard");
    d.setMaidenName("G");
    d.setAcctNum(2);
    d.setPin(22);

    accounts.put(new Integer(d.getAcctNum()), d);

    CustomerProfile e = new CustomerProfile();

    e.setName("Larry Fine");
    e.setMaidenName("M");
    e.setAcctNum(3);
    e.setPin(33);

    accounts.put(new Integer(e.getAcctNum()), e);
    }

    public CustomerProfile getCustomerProfile(int accountNumber) {
    try {

    if (accounts.containsKey(new Integer(accountNumber)) ==
    true) {
    CustomerProfile
    cp = (CustomerProfile)
    accounts.get(new Integer(accountNumber));
    return cp;
    }
    } catch (Exception e) {
    System.out.println("ERROR:" + e);
    }

    return null;
    }


    }
    /*******************************/
    package beans;

    public class CustomerProfile {
    public CustomerProfile(){}
    //i took the static out here and it
    worked
    private static String name;
    private static String maidenName;
    private static int acctNum;
    private static int pin;

    public String getName() {
    return name;
    }

    public String getMaidenName() {
    return maidenName;
    }

    public int getAcctNum() {
    return acctNum;
    }

    public int getPin() {
    return pin;
    }

    public void setName(String name) {
    this.name = name;
    }

    public void setMaidenName(String maidenName) {
    this.maidenName = maidenName;
    }

    public void setAcctNum(int acctNum) {
    this.acctNum = acctNum;
    }

    public void setPin(int pin) {
    this.pin = pin;
    }


    }
    ravi mannan, Nov 23, 2003
    #1
    1. Advertising

  2. ravi mannan wrote:

    > when I execute AccountManager, it's supposed to return an object
    > in the hashmap that corresponds to the key "1", which is the object
    > w/ the name "Moe Howard", instead it always returns the last object
    > put into the HashMap.
    > this is the output:
    > cp.getName:Larry Fine
    > cp.getPin:33
    > cp.getAcctNum:3
    > cp.getMaidenName:M
    >
    > after a long time debugging, I finally got rid of the "static" for the
    > data members
    > in the CustomerProfile class, and got the correct output.
    > cp.getName:Moe Howard
    > cp.getPin:11
    > cp.getAcctNum:1
    > cp.getMaidenName:F


    <snip code>

    Uou need to override the equals(Object o) and hashcode() methods if you're
    using your object in a Map.

    Read chapter 3 of Effective Java, it is explained in there.
    You can find a PDF copy at
    http://developer.java.sun.com/developer/Books/effectivejava/

    --
    Regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Nov 23, 2003
    #2
    1. Advertising

  3. Christophe Vanfleteren wrote:

    > ravi mannan wrote:
    >
    >> when I execute AccountManager, it's supposed to return an object
    >> in the hashmap that corresponds to the key "1", which is the object
    >> w/ the name "Moe Howard", instead it always returns the last object
    >> put into the HashMap.
    >> this is the output:
    >> cp.getName:Larry Fine
    >> cp.getPin:33
    >> cp.getAcctNum:3
    >> cp.getMaidenName:M
    >>
    >> after a long time debugging, I finally got rid of the "static" for the
    >> data members
    >> in the CustomerProfile class, and got the correct output.
    >> cp.getName:Moe Howard
    >> cp.getPin:11
    >> cp.getAcctNum:1
    >> cp.getMaidenName:F

    >
    > <snip code>
    >
    > Uou need to override the equals(Object o) and hashcode() methods if you're
    > using your object in a Map.
    >
    > Read chapter 3 of Effective Java, it is explained in there.
    > You can find a PDF copy at
    > http://developer.java.sun.com/developer/Books/effectivejava/
    >


    Errr, I hadn't read your problem carefully.
    First of all, you only have to implement those methods if you're using your
    objects a a key in a Map.

    But your problem is misunderstanding of the static keyword.
    When you make a field static, that same field is shared with all instances of
    that class. That's why static fields are also called Class fields, because
    they are shared with all instances of the class.

    When you have a class like yours with all static members, you appear to have
    only 1 real (different) object, because all instances share the exact smae
    fields.

    example:

    public class AllStatic {

    private static int x = 1;

    //get & set method

    }

    now if you do
    AllStatic a = new Allstatic();
    Allstatic b = new Allstatic();
    a.setX(11);
    //b.getX() will also return 11, even if you haven't changed it in b.

    If you don't make x static, each instance will have its own value for x

    So the same code would result in:
    AllStatic a = new Allstatic();
    Allstatic b = new Allstatic();
    a.setX(11);
    //b.getX() will still return 1, as it has its own value for x.

    --
    Regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Nov 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince Darley
    Replies:
    4
    Views:
    4,368
    emilchacko
    Mar 2, 2010
  2. Olivier Wulveryck
    Replies:
    1
    Views:
    654
    Andrew Thompson
    Sep 14, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,669
    Smokey Grindel
    Dec 2, 2006
  4. Rakesh
    Replies:
    10
    Views:
    12,132
    Mike Schilling
    Apr 8, 2008
  5. Daniel Waite
    Replies:
    2
    Views:
    209
    Daniel Waite
    May 2, 2008
Loading...

Share This Page