E109: Static Fields Only Allowed in Objects
This error is emitted when the @static annotation is used on a member that is not inside an object.
The @static annotation can only be applied to members defined within a Scala object, not in classes or traits.
Example
import scala.annotation.static
class Example:
@static val count = 0
Error
-- [E109] Syntax Error: example.scala:4:14 -------------------------------------
4 | @static val count = 0
| ^^^^^^^^^^^^^^^^^^^^^
|@static value count in class Example must be defined inside a static object.
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| @static members are only allowed inside objects.
-----------------------------------------------------------------------------
Solution
// Move the @static member to an object
import scala.annotation.static
class Example
object Example:
@static val count = 0
// Or remove @static if you don't need static behavior
class Example:
val count = 0
In this article