3

I want to instantiate object for an enum. For example:

public enum Type {

  TYPE1("type_name_1", new TypeObj1()), 
  TYPE2("type_name_2", new TypeObj2());

  TypeInterface typeObj;
  String value;
}
public class TypeObj1 implements TypeInterface {
  ...
}
public class TypeObj2 implements TypeInterface {
  ...
}

Is this a correct way to do it? I want a single object of TypeInterface attached to enum. Please suggest alternatives.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Pandey
  • 91
  • 8
  • 1
    What do you mean by tied to an enum? Can `TYPE1` have `TypeObj2` for example? – davidalayachew May 24 '23 at 15:31
  • It is very unclear what you are trying to achieve. Does this question about using generics with enum help? https://stackoverflow.com/a/11490505/721855 – aled May 24 '23 at 15:38
  • @davidalayachew no TYPE1 would only have TypeObj1 – Pandey May 24 '23 at 15:58
  • @aled I wanted to know if the approach I am using is correct or not. I would like to instantiate object of class implementing TypeInterface in Enum constructor. Is this right practice? – Pandey May 24 '23 at 16:01
  • Then my next question, you said that `TYPE1` can only interact with `TypeObj1`. Is this something that you want ***enforced*** by the compiler? Or do you just want to map the relationship and see if there is a better way to do it? – davidalayachew May 24 '23 at 16:05

2 Answers2

1

Yes, you are headed in the right direction.

You’ll need a constructor to assign the passed objects to your member fields.

Make your member fields final to prevent their reassignment to another object reference.

public enum Type {
  // Enum objects.
  TYPE1("type_name_1", new TypeObj1()), 
  TYPE2("type_name_2", new TypeObj2());

  // Member fields.
  final TypeInterface typeObj;
  final String value;

  // Constructor.
  Type( final String value , final TypeInterface typeInterface ) {
    this.typeObj = typeInterface ;
    this.value = value ;
  }
}

Tip: This kind of thought exercise is more clear and productive if you devise a simple but semi-realistic scenario rather than your amorphous “TypeInterface” and “value”.

public enum Pet {
  // Enum objects.
  FLUFFY( "tuxedo" , new Cat() ), 
  ROVER( "chocolate Lab" , new Dog() );
  
  // Member fields.
  final Animal animal;
  final String description;

  // Constructor.
  Type( final String desc , final Animal animal ) {
    this.animal = animal ;
    this.description = desc ;
  }
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Create a constructor for your enum, which matches the arguments you're specifying.
There is no access-modifier; and private is redundant.

Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects).

public enum Type {
    TYPE1("type_name_1", new TypeObj1()),
    TYPE2("type_name_2", new TypeObj2());

    final TypeInterface typeObj;
    final String value;
    
    Type(String value, TypeInterface typeObj) {
        this.value = value;
        this.typeObj = typeObj;
    }
}
Reilas
  • 3,297
  • 2
  • 4
  • 17