java - How does this class work or how can you use it? -
i fell on class looks this:
public final class databasetype{ public static final databasetype type_limited_text = new databasetype(); public static final databasetype type_unlimited_text = new databasetype(); public static final databasetype type_date = new databasetype(); public static final databasetype type_decimal = new databasetype(); private databasetype(){ } public string tostring(){ return "databasetype"; } } i need set type want understand what's happening here , have no clue how class works. whatever variable use return empty databasetype, no information. wonder how can use of such class. maybe there name type of class?
basically, class lists 4 enumerable constants, can use in method signatures
public databasetype gettypeofdb(); in client code, you'll have type-safe way compare constants:
if (gettypeofdb() == databasetype.type_limited_text) { dosomething(); } even though implementation seems bit clumsy, quite closely emulates java 5 enum, gimby pointed out in comments. ideas in design following:
- the constructor
private, meaningpublic static final databasetypeinstances declared within class can exist - the class
finalcannot work around above restriction adding more constants in subclass - the constant fields in class have strong typing, i.e. not
ints, insteaddatabasetypes, helps eliminate bugs caused typos or "magic numbers" in client code
the modern way same using enum instead:
public enum databasetype { type_limited_text, type_unlimited_text, type_date, type_decimal; }
Comments
Post a Comment