title: Force initialization of Scala singleton object
body: <p>I'm working on an automatic mapping framework built on top of Dozer. I won't go into specifics as it's not relevant to the question but in general it's supposed to allow easy transformation from class A to class B. I'd like to register the projections from a class's companion object.</p>
  
  <p>Below is a (simplified) example of how I want this to work, and a Specs test that assures that the projection is being registered properly.</p>
  
  <p>Unfortunately, this doesn't work. From what I can gather, this is because nothing initializes the A companion object. And indeed, if I call any method on the A object (like the commented-out hashCode call, the projection is being registered correctly.</p>
  
  <p>My question is - how can I cause the A object to be initialized automatically, as soon as the JVM starts? I don't mind extending a Trait or something, if necessary.</p>
  
  <p>Thanks. </p>
  
  <pre><code>class A {
    var data: String = _
  }
  
  class B {
    var data: String = _
  }
  
  object A {
    projekt[A].to[B]
  
  }
  
  "dozer projektor" should {
    "transform a simple bean" in {
  //    A.hashCode
        val a = new A
        a.data = "text"
        val b = a.--&gt;[B]
  
        b.data must_== a.data
    }
  }
  </code></pre>