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
Constants are best used with simple data type when the value will never change.

Read-Only Field

  • Runtime constant 
  • Assigned to any valid expression at runtime
  • Assign on declaration or constructor 
  • any data type
  • optionally static
Read-only fields are best for any values that initialized from a file, a table, or same code, but cannot then be changed anywhere else within the application.
If the read-only value is shared by instances, then make it as a static read-only field.

Comments

Popular posts from this blog

Constants

FAQ: