What is the best way to link information to the class fields?

Viewed 45

In this context i mean information in a sense of "static" information. Something like a displayname of a field, which can be used in many different places (for example as a column title for a table or as a hint for a text view).

Lets assume we have following POJO:

class Person
{
    private String name;
    private int age;
}

Now i want to add some information to the fields of the POJO with following interface:

public interface Field<T>
{
    String getDbName();
    String getDisplayName();
    Validator getValidator();
    View createTableCell(T data);
    View createInput(T data);
}  

Each field of the POJO gets listed in a class called PersonFields:

class PersonFields 
{
    public static final NAME = new Field<Person>()
    {
        @Override
        public String getDisplayName()
        {
             return Utils.getString(R.string.person_name);
        }

        ...
    }
}

Is this considered bad practice? Or is there some other way to archive something like this?

1 Answers

It's never a good idea to use static in Java.

If you really need to keep single state in your application, use the anti-pattern Singleton.

Related