Should Java enum constants be retrieved through "get" methods or public final fields or both? -
see below code example:
public enum employees { bob("bob barker", "bb", 100); private string fullname; //are these better accessed through public or getfullname() below? private string initials; private string age; employees(string fullname, string initials, int age) { this.fullname = fullname; this.initials = initials; this.age = age; } public string getfullname() { return fullname; } //etc ... }
which method of accessing more correct or more memory efficient?
you cannot access fullname
through static
method. instance fields.
your code correct. may wish mark string fields final
, rid of setxxx
methods (since enum
values traditionally immutable).