1

Does scala have something similar like what class loader does to static block in java?

for e.g. something similar to below in scala:

class A{
static{

System.out.println("This gets called at the time of loading a class by class loader.")
}
}

I am using Scala 2.x with Apache Spark 2.x

PS: I have already read What is Scala equivalent of Java's static block? this answer, but I don't want to create companion object and then call it through class's constructor.

Edit: [My Use Case]

Consider a scenario of java where we write Class.forName("some.jdbc.driver") inside the static block and then put the jdbc driver in a class path. After that class loader loads the class we mentioned. I want to do something exactly like that

nomadSK25
  • 2,350
  • 3
  • 25
  • 36

2 Answers2

5

Edit: No one offered much hope, so here's a link to the SIP for @static members. It's already implemented for Dotty/Scala 3.

However, the members are defined on the companion. The RHS of member definitions can contain arbitrary code, so there's no need for syntax for static initializers as such.

Still wondering what's the use case.

What is your use case?

Ordinarily:

scala 2.13.0-M4> object X { println("hi") }
defined object X

scala 2.13.0-M4> X
hi
res0: X.type = X$@554c4eaa

scala 2.13.0-M4> :javap -c X
Compiled from "<console>"
public class $line3.$read$$iw$$iw$X$ {
  public static $line3.$read$$iw$$iw$X$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class $line3/$read$$iw$$iw$X$
       3: invokespecial #20                 // Method "<init>":()V
       6: return

  public $line3.$read$$iw$$iw$X$();
    Code:
       0: aload_0
       1: invokespecial #21                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: putstatic     #23                 // Field MODULE$:L$line3/$read$$iw$$iw$X$;
       8: getstatic     #28                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      11: ldc           #30                 // String hi
      13: invokevirtual #34                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      16: return
}

To convince you that this is ordinary static loading:

scala 2.13.0-M4> :pa -raw
// Entering paste mode (ctrl-D to finish)

package y { object Y { println("hi") } }

// Exiting paste mode, now interpreting.


scala 2.13.0-M4> Class.forName
   def forName(x$1: String,x$2: Boolean,x$3: ClassLoader): Class[_]   def forName(x$1: String): Class[_]

scala 2.13.0-M4> Class.forName("y.Y$", true, getClass.getClassLoader)
hi
res5: Class[_] = class y.Y$
som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Consider a scenario of java where we put Class.forName("some.jdbc.driver") inside static block and jdbc driver is in class path.After that class loader loads class we mentioned. I want to do something exactly like that. So your answer is irrelevant. BTW thnx for point out to SIP. – nomadSK25 Jul 18 '18 at 03:27
  • I forgot to mention that I am doing spark-submit. – nomadSK25 Jul 19 '18 at 06:49
1

I think the answer you are referencing is the only answer you'll get.

If you have code like the below, you still need to reference the object in some way to get the init code to run.

object StaticBlock {
  println("init")
}

This is the same in Java: unless you load the class, static blocks in that class are not executed.

daniel kullmann
  • 13,653
  • 8
  • 51
  • 67