Skip to content

object member variable initialization #9360

@scabug

Description

@scabug
class BaseClass(s: String) {
  def print: Unit = {
    println("hashCode=" + this.hashCode + " | " + this.getClass.getSimpleName + " " + s)
    println(this.getClass.getDeclaringClass.getName)
  }
}

object Object {
  val s: String = "hello"

  object AObj extends BaseClass(s)

  object BObj extends BaseClass(s)

  val list = List(AObj, BObj)

  def print() {
    println(list)
  }
}

object ObjectInit {
  def main(args: Array[String]) {
    Object.AObj.print
    Object.BObj.print
    Object.print
  }
}

Output: AObj in list is null.
List(null, fun.Object$BObj$@28d93b30)
Expected: initialization of list should be initialized correctly independent from Object.AObj.print/Object.AObj.print.

Note: If No Object.AObj.print/Object.AObj.print be called before Object.print, the initialization is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions