I have a situation where I have a User that is a certain profession, so I have something like:
public abstract class Job
{
private static String name;
public static String getJobName() { return name; }
}
and
public class Baker extends Job
{
// I realize this is probably wrong, but not the main point
Baker() { name = "Baker"; }
}
Then i wanted to store the User->Profession in a map like
Map<User, Class<? extends Job>> uJobs = HashMap<User, Class<? extends Job>>();
uJobs.put(user, Baker.class); // This seems to work
String jobName = uJobs.get(user).getJobName(); // This not so much, and the IDE (eclipse) just somes 'class' specific stuff, not Job specific stuff.
Any advice is appreciated. The core goal here is to map 1 Object to a Class that I can just call static methods from without needing an instance for every Object.
Don’t put the class object in the hash map, just put an instance of the class.
Better still, you probably don’t need to create a new instance of Baker each time you add a user. Make a static or a singleton.
If the only thing that distinguishes the jobs is data, you don’t need to subclass. You could instead say