android - can I read public static final field inside a nested public static abstract class in Java -
so while created sqlitehelper class in android app. i'm not 100% why, table , column names public static final fields in nested public static abstract class. recall, goal protect fields being modified. works great things starting sophisticated app , want populate fields in other classes public static final table , column name fields. after trial , error, , reading abstract classes, static classes, , nesting classes, occurred me can call field directly, in.
string mytable = mysqlitehelper.dbfields.table_name_reminder;
even though i've read on these topics, how comes in specific case still making me scratch head. question is, since fields static final, nesting fields provide additional protection, , if nested class static, why make abstract? static , abstract required call directly without needing instantiate outer , nested classes?
thanks help. i'm trying head wrapped around various class implementations.
here's key elements of class :
public class mysqlitehelper extends sqliteopenhelper { private static final string database_name = "reminders.db"; private static final int database_version = 5; public int databaseversion = database_version; public mysqlitehelper(context context) { super(context, database_name, null, database_version); } public static abstract class dbfields implements basecolumns { //dbfields table fields public static final string table_name_reminder = "reminders"; public static final string column_reminder_id = _id; public static final string column_reminder = "reminder"; public static final string column_reminder_altitude = "altitude"; public static final string column_reminder_used = "is_used"; public static final string column_reminder_lastused = "last_used"; public static final string column_reminder_action = "action"; public static final string column_reminder_score = "score"; public static final string column_reminder_relationship = "relationship"; //special_days table fields public static final string table_name_special_days = "special_days"; public static final string column_special_days_id = _id; public static final string column_special_days_date = "date"; //dbdatarow strfield 1 public static final string column_special_days_name = "name"; //dbdatarow dbdata public static final string column_special_days_altitude = "altitude"; //dbdatarow intfield 1 public static final string column_special_days_used = "is_used"; //dbdatarow field 2 public static final string column_special_days_warning = "warning"; //dbdatarow intfield 3 public static final string column_special_days_action = "action"; //dbdatarow intfield 4 } }
since fields static final, nesting fields provide additional protection
no, doesn't. you've seen, can access fields if they're nested and, since they're static , final, can't modify them.
and if nested class static, why make abstract? static , abstract required call directly without needing instantiate outer , nested classes?
the purpose of abstract allow have base class has method no implementation. 1 classic example animal class. animals make noise (probably not, let's pretend) animal class should have makenoise
method. but, every animals noise different doesn't make sense have implementation in base class. cat
subclass might public string makenoise() { return "meow"; }
, dog
subclass might return "woof", there's no sane implementation of makenoise
on base class. however, if didn't have makenoise
on base class couldn't ask arbitrary animal makenoise
. you'd have public abstract string makenoise()
method on base. lets call makenoise
animal if have reference animal
.
note abstract has nothing class being nested or not. other classes, nested or not, can inherit nested static class. has nothing hiding data or otherwise protecting data or code.
so, answer question: should make abstract if , if purpose of being abstract applies here. given sample code, doesn't.
Comments
Post a Comment