Back to Scala3

E116: Missing Companion for Static

docs/_docs/reference/error-codes/E116.md

3.8.41.5 KB
Original Source

E116: Missing Companion for Static

This error is emitted when an object contains @static members but does not have a companion class.

Objects with @static members must have a companion class because the static members are placed in the companion class's bytecode.


Example

scala
import scala.annotation.static

object Example:
  @static val count = 0

Error

scala
-- [E116] Syntax Error: example.scala:4:14 -------------------------------------
4 |  @static val count = 0
  |  ^^^^^^^^^^^^^^^^^^^^^
  |  object Example does not have a companion class
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | An object that contains @static members must have a companion class.
   -----------------------------------------------------------------------------

Solution

scala
// Add a companion class
import scala.annotation.static

class Example

object Example:
  @static val count = 0
scala
// Or remove @static if you don't need static behavior
object Example:
  val count = 0
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>