Constant vs Read-Only
Constant vs Read-Only
Constant Field
- Compile-time constant
- meaning that it must be assigned to an expression that can be fully evaluated at compile-time.
- Assigned on declaration
- only number, boolean, or string
- always static
Read-Only Field
- Runtime constant
- Assigned to any valid expression at runtime
- Assign on declaration or constructor
- any data type
- optionally static
If the read-only value is shared by instances, then make it as a static read-only field.
Comments
Post a Comment