Kacper Sierakowski
Kacper Sierakowski

Reputation: 393

How can I create enum type with dataType (String, Integer, boolean) in Android Studio using Java?

I have an object with String values. And in one parameter of this object I store dataType of the other parameter. I was wondering if there is a way to create enum type like this:

public enum KeyType {
    String,
    Integer,
    boolean
}

I have an application with all settings of the different apps installed on smartphone. And I have a service which stores the whole data. I sent objects to this service from other apps. And I am trying to avoid creating three types of this object because only difference will be this one property type. But I assume this is the only way to do it.

Upvotes: 0

Views: 521

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

You can add a Class object in your enum and add the type like this

public enum KeyType {
    String(String.class), Integer(Integer.class), Boolean(Boolean.class);

    KeyType(Class _class){mClass = _class;}

    private Class mClass;

    public Class getClassType(){return mClass;}
}

and you can use it like

KeyType k = KeyType.String;
Class class = k.getClassType();
boolean type = k.getClassType() instanceof String; // true

Upvotes: 3

Related Questions