views:

81

answers:

4

I have a Employee class and department class.Now within employee i have a department as its member.The department can be either of 2 type "HR" or "Admin".Now should i declare departmentype as a enum in a separate interface and then should model department class as show below?

public interface Myconstants{

  enum Depttype{HR,Admin};

}

public class Department{

 Myconstants.Depttype depttype;
 Department(Myconstants.Depttype depttype){
   this.deptype = deptype;
}
+1  A: 

I would simply declare the enum on the Department class.

public class Department {

  public enum Depttype{HR,Admin};

  private Depttype depttype;

  Department(Depttype depttype) {
   this.deptype = deptype;
  }
}
eljenso
which practice would be better? to declare constants in seperate interface like i have done, or like u have done?
akshay
Yes, absolutely
Maurice Perry
It's much better done in the Department class.
Maurice Perry
@Maurice :Am confused.Which s better,Myne or eljensos?
akshay
`Depttype` is the type of a department, so it is naturally tied to `Department`. No need to create a separate "global" constants-declaring type.
eljenso
thanks all.Can you please help me on the new commnet posted by me for Nicolas answer
akshay
Don't ask questions in the comments like that. You already know where the "Ask Question" button is.
eljenso
A: 

As written, why not just declare an enum?

You're declaring an enum and then wrapping it in a class, for no clear benefit.

Visage
thanks.Can you please help me on the new commnet posted by me for Nicolas answer
akshay
A: 

I don't see why the enum should be part of an interface. If the "Department type" as you intend it is supposed to be a caracterization of your "Departments", then the proper way seems to declare the enum as attribute of the Department class.

Nicolas C.
ok, thanks.Now i have one more doubt.I want to create one instance of HR and one instance of Admin department when my application loads(console app) and use those instances throughout my program.How can i use same instances everywhere in different classes?.For example i read a employee from csv file and then create a employee object.Now to create a employee object i must use the department object.I have to set proper value of department depending on the value of department read from file.How to do it?
akshay
You seem new to Java. You should read tutorials first here : http://download.oracle.com/javase/tutorial/java/javaOO/constructors.html, but basically, to do that, you :1) read your document, create class `Employee e; Department d;` 2) create for each employee `e = new Employee();`and `d = new Department(<type>);`3) bind the department to employee :`e.setDepartment(d);`or, depending how you made the constructors :`e = new Employee(new Department(HR));`
Nicolas C.
A: 

We declare enums separately, as straightforward enums in their own constants directory. Departmenttype could in the future be used elsewhere in the app ? And keeping all the constants/enums in the same place makes it easy for the developers.

NimChimpsky