Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > why did this hashmap always returned the same object?

Reply
Thread Tools

why did this hashmap always returned the same object?

 
 
ravi mannan
Guest
Posts: n/a
 
      11-23-2003
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;
}


}
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      11-23-2003
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/develo...effectivejava/

--
Regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      11-23-2003
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/develo...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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reuse HashMap$Entry (or HashMap in total) to avoid millions of allocations Vince Darley Java 4 03-02-2010 07:48 AM
java.util.Properties extending from HashMap<Object, Object> insteadof HashMap<String, String> Rakesh Java 10 04-08-2008 04:22 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Problem with XPath JDOM: Always the same value returned when selecting different nodes. Olivier Wulveryck Java 1 09-14-2004 01:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57