Hello.
I want to write the correct code, I wondered. After all, where is it better to store constants in Java? Now I pick the codes of standard Android applications. I met for myself a few controversial moments. I want to ask a few questions:
- Where is it better and better to store constants that are often used from other classes?
- What are
private
/protected
interface
? They, in turn, contain constants. For example:
protected interface AlarmsColumns extends AlarmSettingColumns, BaseColumns
And the class in which such interfaces are declared is called Contract(ClockContract)
. The constants themselves in the interface are declared as public static final
. Initially, in the program code, constants were used like this:
ClockContract.InstancesColumns._ID
Everything was good, if used from a class in one package, and if the class is in another package, when trying to access, the IDE wrote a logical error about access to protected
. You can also entrench only if you are in the same package. Although, after reading the official docks and the Internet, storing constants in the interface is considered to be antipattern.
Explain, please, why was this done? How do protected
/ private
interface
and where is it better to store constants?
Thanks in advance.