why did this hashmap always returned the same object?

R

ravi mannan

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


}
 
C

Christophe Vanfleteren

ravi said:
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/
 
C

Christophe Vanfleteren

Christophe said:
<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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top