Your example code:
public enum WorkerType{
WorkerType(int id, String name){
this.id = id;
this.name = name;
}
int id;
String name;
}
… confuses your question. You are mixing your worker type with your actual workers.
Likely the code appropriate to your situation is a class to represent each worker.
public record Worker( int id , String name , Classifation classifation ) {}
The type of worker is a separate enum, with only two named objects (one for full-time, one for part-time), and no need for a custom constructor.
public enum Classifation { FULL_TIME, PART_TIME }
I would nest the type-of-worker enum within the worker class. That nesting makes clear that the type-of-worker enum objects are mainly used only within that class.
public record Worker( int id , String name , Classifation classifation )
{
public enum Classifation { FULL_TIME, PART_TIME }
}
Usage:
Worker adam = new Worker( 1 , "Adam" , Worker.Classifation.FULL_TIME );
Worker milne = new Worker( 2 , "Milne" , Worker.Classifation.PART_TIME );
System.out.println( List.of( adam , milne ) );
When run.
[Worker[id=1, name=Adam, classifation=FULL_TIME], Worker[id=2, name=Milne, classifation=PART_TIME]]
Your title asks:
Reference Table OR Java Enum which is better
There are at least two factors to consider:
- Will the infrequent changes happen only while you are programming your Java app?
- Will other apps or the DBA need to do lookups on this data?
An enum in Java is for automatically instantiating a limited set of named objects known at compile-time. You cannot change the enum at runtime.
If there is any possibility of this information changing while your app is deployed, then an enum is not appropriate. Instead, you need to dynamically create a collection of objects during runtime.
If there is any possibility of other apps, or the DBA, needing to look up this information, then you should record that info within the database rather than within your Java app.
Given your example scenario, I suspect you may want both enums in Java and look-up reference table in database.
Another approach is to use human-readable text instead of a plain integer as a WORKER_TYPE column. In your case, store textual values such as Full-time & Part-time, or Full & Part. In this case, no need for the loop-up reference table. This approach assumes (A) there is no business need for the type code (1 or 2), and (B) there are no further columns to track in describing a type of worker.
Furthermore, in more sophisticated databases, you can have the database enforce the limited set of possible values being stored in that column.