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