3

When and how is the Scala Package object constructor called?

I have two classes:

Package.scala

package my;
package object entities {
   //Some initialization code in constructor
}

Classy.scala

package my.entities;
case class Classy {
}

I am trying to have the entities constructor to have already been executed by the time an object of Classy is created.

4
  • you could try to put two println in the initialization of the package object and case class and see in what order they get printed to screen. Commented Dec 29, 2012 at 10:14
  • Actually the "entities" constructor doesn't get called at all. I want to know, how to call it. I would assume it should be called automatically whenever a package class is created or imported...? Commented Dec 29, 2012 at 10:25
  • it's created (and initialized) when it's first used. Try doing val c = Classy . See also stackoverflow.com/questions/10973692/… Commented Dec 29, 2012 at 10:52
  • I created an object of Classy but it doesn't get called. Commented Dec 29, 2012 at 11:28

1 Answer 1

5

The package object is translated into a normal java class file called package.class IIRC. From then on I assume it behaves like any normal Java class, thus it is loaded and instantiated when it is first referenced. In Scala, that means you need to define some method or val in the package object, then access it from outside. In your case, you may try calling it from the constructor of Classy, or from the code which instantiates Classy.

Update

OK, here is some code I ran to test what I described above:

// package.scala
package some

package object pkg {
  println("package created!")

  def func() { println("func called") }
}

// C.scala
package some.pkg

class C {
  println("created C")
}

// G.scala
package some.pkg

object G {
  println("creating G")
  func()
  println("created G")
}

// PackageTester.scala
package some.pkg

object PackageTester extends App {
  val c = new C
  val g = G
}

And the output is:

created C
creating G
package created!
func called
created G

Which proves that Scala package objects are created lazily, only when they are actually referenced. And in fact the same is true for "normal" Scala objects, as demonstrated by G above.

Sign up to request clarification or add additional context in comments.

7 Comments

As it is an object and not a class, shouldn't the constructor be called automatically?
Objects are lazy. They're not instantiated until they are used. I would assume the same applies for package objects. Try using the (package) object, and the constructor should be called.
Your program seems side-effect heavy. What exactly are you trying to do? This sounds "dangerous" :P
Doesn't creating an object of a class within a package mean using package object?
@0n4li, definitely not. Terminology wise, they sound as if they were related, but package objects are more like a convenience to "emulate" free (global) functions in Scala. And if you take into account the Java implementation of these, it is clear that there is no implicit dependency between a class/object residing in a package, and the corresponding package object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.