Skip to content

Instantly share code, notes, and snippets.

@Krasnyanskiy
Last active September 28, 2015 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krasnyanskiy/0d4b5e02c169cc76e2ba to your computer and use it in GitHub Desktop.
Save Krasnyanskiy/0d4b5e02c169cc76e2ba to your computer and use it in GitHub Desktop.

How Scala compiled class would look like if we decompile it?

class Foo {
  private var bar = 0
  private[this] var bazz = 0
}

object Foo {
  def main(args: Array[String]) {
    val foo = new Foo
    val bar = foo.bar
    println(bar)
  }
}

It could be

public class Foo {

    private int Foo$$bar = 0;
    private int bazz = 0;

    public static void main(String[] args) {
        Foo$.MODULE$.main(args);
    }

    // getter
    public int Foo$$bar() {
        return this.Foo$$bar;
    }

    // setter
    private void Foo$$bar_$eq(int x$1) {
        this.Foo$$bar = x$1;
    }
}

Plus singleton

import scala.Predef;
import scala.runtime.BoxesRunTime;

public final class Foo$ {
    public static final Foo$ MODULE$;

    static {
        MODULE$ = new Foo$();
    }

    public void main(String[] args) {
        Foo foo = new Foo();
        int bar = foo.Foo$$bar();
        Predef.println((Object) BoxesRunTime.boxToInteger((int) bar));
    }

    private Foo$() {
        //
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment