I have a class with a private static HashSet defined in it that holds objects, when I pass a new object to the class all the elements of the HashSet change to the last inserted objects. I thought a static declared Hashset is only created once so any objects added to it from a instance of the class would stay there
package dao;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import domain.Category;
public class CategoryDAO {
private static Set<Category> categories = new HashSet<Category>();
// setter for products
public void store(Category category){
categories.add(category);
//System.out.println(categories.toString() +"in array after add");
}
//testing the static hash set
public void displayHashSet(){
Object o = categories;
if(o instanceof Category) {
Category category = (Category) o;
System.out.println(category.getName()+" "+category.getDescription());
}
}
// getter for products
public Set<Category> getAll(){
return categories;
}
}