Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:44:00+00:00 2026-06-12T12:44:00+00:00

object ScalaTrueRing { def rule = println(To rule them all) } this piece of

  • 0
object ScalaTrueRing {
  def rule = println("To rule them all")
}

this piece of code will be compiled into java byte code, if I decompile it, then the equivalent Java code is similar to this:

public final class JavaTrueRing
{
  public static final void rule()
  {
    ScalaTrueRing..MODULE$.rule();
  }
}


/*    */ public final class JavaTrueRing$
/*    */   implements ScalaObject
/*    */ {
/*    */   public static final  MODULE$;
/*    */ 
/*    */   static
/*    */   {
/*    */     new ();
/*    */   }
/*    */ 
/*    */   public void rule()
/*    */   {
/* 11 */     Predef..MODULE$.println("To rule them all");
/*    */   }
/*    */ 
/*    */   private JavaTrueRing$()
/*    */   {
/* 10 */     MODULE$ = this;
/*    */   }
/*    */ }

it’s compiled into two classes, and if I use Scala.net compiler, it’ll be compiled into MSIL code, and the equivalent C# code is like this:

public sealed class ScalaTrueRing
{
    public static void rule()
    {
        ScalaTrueRing$.MODULE$.rule();
    }
}

[Symtab]
public sealed class ScalaTrueRing$ : ScalaObject
{
    public static ScalaTrueRing$ MODULE$;
    public override void rule()
    {
        Predef$.MODULE$.println("To rule them all");
    }
    private ScalaTrueRing$()
    {
        ScalaTrueRing$.MODULE$ = this;
    }
    static ScalaTrueRing$()
    {
        new ScalaTrueRing$();
    }
}

It’s also compiled into two classes.

Why do Scala compilers(the one for Java and the one for .NET) do this?
Why does not it just call the println method in the static rule method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:44:02+00:00Added an answer on June 12, 2026 at 12:44 pm

    It is important to understand that in scala, an object actually is a first class citizen: it is an actual instance that can be passed around as any other object.
    By example:

    trait Greetings {
      def hello() { println("hello") }
      def bye() { println("bye") }
    }
    
    object FrenchGreetings extends Greetings {
      override def hello() { println("bonjour") }
      override def bye() { println("au revoir") }
    }
    
    def doSomething( greetings: Greetings ) {
      greetings.hello()
      println("... doing some work ...")
      greetings.bye()
    }
    
    doSomething( FrenchGreetings )
    

    Unlike with static methods, our singleton object has full polymorphic beheviour. doSomething will indeed call our overriden hello and bye methods, and not the default implementations:

    bonjour
    ... doing some work ...
    au revoir
    

    So the object implementation must necessarily be a proper class. But for interoperability with java,
    the compiler also generates static methods that just forward to the unique instance (MODULE$) of the class (see JavaTrueRing.rule()).
    This way, a java program can access the methods of the singleton object as a normal static method.
    Now you might ask why scala does not put the static method forwarders in the same class as the instance methods. This would give us something like:

    public final class JavaTrueRing implements ScalaObject {
      public static final  MODULE$;
    
      static {
        new JavaTrueRing();
      }
    
      public void rule() {
        Predef.MODULE$.println("To rule them all");
      }
    
      private JavaTrueRing() {
        MODULE$ = this;
      }
    
      // Forwarders
      public static final void rule() {
        MODULE$.rule();
      }  
    }
    

    I believe that the main reason why this can’t be as simple is because in the JVM you cannot have in the same class an instance method and a static method wth the same signature.
    There might be other reasons though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Object[] array = new Object[]{}; System.out.println((array instanceof Serializable));//passed System.out.println((array instanceof Cloneable));//passed This code compiles
object Initialization { def main(args: Array[String]): Unit = { val list = Traversable(1,2,4,5) //list.foreach(println)
object TestClass { def main (args: Array[String]) { println(Hello World); val c = List
Object doesn't support this property or method It's this line. pthumb = $(#pthumb).attr(src); Does
object ReassignTest extends App { class X(var i : Int) def x = new
object here should refer to the Object class. As with all class identifiers in
Good object-oriented design says that objects should not expose their internals. Given this is
Object-Oriented programmers seem to have all the fun. Not only are they treated to
Object o1 = new Object(); Object o2 = new Object(); //o1=o2; System.out.println(o1.equals(o2)); It returns
Object reference not set to an instance of an object. i am getting this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.