What is the Java equivalent of a Scala object?
Asked Answered
C

3

13

In Scala, we can write

object Foo { def bar = {} }

How is this implemented by the compiler? I am able to call Foo.bar(); from Java but new Foo(); from Java gives the error cannot find symbol symbol: constructor Foo()

  • Does the JVM support singletons natively?
  • Is it possible to have a class in Java that does not have a constructor?

Note: here is the code output by scalac -print

package <empty> {
  final class Foo extends java.lang.Object with ScalaObject {
    def bar(): Unit = ();
    def this(): object Foo = {
      Foo.super.this();
      ()
    }
  }
}
Clathrate answered 19/10, 2011 at 10:26 Comment(0)
E
12

Support for singletons is not on a language level, but the language provides enough facilities to create them without any trouble.

Consider the following code:

public class Singleton {
    private static final Singleton instance = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

This is an example from Wikipedia, which explains how a singleton can be made. An instance is kept in a private field, constructor is inaccessible outside the class, the method returns this single instance.

As for constructors: every class by default has a so-called default constructor which takes no arguments and simply calls the no-args constructor of the superclass. If the superclass doesn't have any accessible constructor without arguments, you will have to write an explicit constructor.

So a class must have a constructor, but you don't have to write it if the superclass has a no-args constructor.

Eclogite answered 19/10, 2011 at 10:36 Comment(1)
geeksforgeeks.org/singleton-class-java/?ref=lbp talks about ensure only 1 instance of the Singleton is created.Feeler
H
23

When compiling your code, Scala compiler produces an equivalent of the following Java code:

public final class Foo {
    private Foo() {} // Actually, Foo doesn't have constructor at all
                     // It can be represented in bytecode, 
                     // but it cannot be represented in Java language

    public static final void bar() {
        Foo$.MODULE$.bar();
    }
}

public final class Foo$ implements ScalaObject {
    public static final Foo$ MODULE$;
    static {
        new Foo$();
    }
    private Foo$() { MODULE$ = this; }
    public final void bar() {
        // actual implementation of bar()
    }
}

Here Foo$ is an actual implementation of a singleton, whereas Foo provides a static method for interaction with Java.

Helban answered 19/10, 2011 at 10:44 Comment(3)
Is there any way to see the "intermediate" Java code, as you have shown? Did you use a decompiler to generate it?Clathrate
@Jus12: Perhaps some decompilers are able to show it, but I reconstructed it manually from the output of javap -c -private Foo / javap -c -private Foo$Helban
Hi @axtavt, what is this part called? static { new Foo$(); } other members(methods fields) all have a name. what type of member is this?Feeler
E
12

Support for singletons is not on a language level, but the language provides enough facilities to create them without any trouble.

Consider the following code:

public class Singleton {
    private static final Singleton instance = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

This is an example from Wikipedia, which explains how a singleton can be made. An instance is kept in a private field, constructor is inaccessible outside the class, the method returns this single instance.

As for constructors: every class by default has a so-called default constructor which takes no arguments and simply calls the no-args constructor of the superclass. If the superclass doesn't have any accessible constructor without arguments, you will have to write an explicit constructor.

So a class must have a constructor, but you don't have to write it if the superclass has a no-args constructor.

Eclogite answered 19/10, 2011 at 10:36 Comment(1)
geeksforgeeks.org/singleton-class-java/?ref=lbp talks about ensure only 1 instance of the Singleton is created.Feeler
M
4

Joshua Bloch recommened in the book "Effective Java" the use of an enum to implement a singleton.

See this question: What is an efficient way to implement a singleton pattern in Java?

Marela answered 19/10, 2011 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.