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

  • SEARCH
  • Home
  • 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 7555585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:38:50+00:00 2026-05-30T11:38:50+00:00

A companion object for a trait in Scala has no visibility problems in Scala

  • 0

A companion object for a trait in Scala has no visibility problems in Scala:

trait ProtocolPacket extends Serializable {    
  def toByteArray: Array[Byte]
}

object ProtocolPacket {
  def getStreamType( streamBytes: Array[Byte] ) = {
    // ...
  }
}

However on Java side (e.g. gets the above in a jar), a ProtocolPacket.getStreamType is not visible. In fact a (decompiled by IDEA) source does not have a getStreamType method defined for a ProtocolPacket

EDIT:

I found similar hits on SO regarding Companion$MODULE$, but was tricked by IDEA 🙂 as shown below:

enter image description here

The above compiles and runs fine (shell and/or IDEA itself), in case anybody else gets trapped.

  • 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-05-30T11:38:52+00:00Added an answer on May 30, 2026 at 11:38 am

    Looking at javap output you will find:

    $ javap ProtocolPacket
    public interface ProtocolPacket extends scala.Serializable{
        public abstract byte[] toByteArray();
    }
    

    and companion object:

    $ javap ProtocolPacket$
    public final class ProtocolPacket$ extends java.lang.Object implements scala.ScalaObject,scala.Serializable{
        public static final ProtocolPacket$ MODULE$;
        public static {};
        public void getStreamType(byte[]);
        public java.lang.Object readResolve();
    }
    

    this makes me believe in Java you can write:

    ProtocolPacket$.MODULE$.getStreamType(/**/)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need the object (or singleton object or companion object... anything but the class)
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
I write object MyString { implicit def stringToMyString(s: String) = new MyString(s) } class
Consider this code: object A { object AA extends A object AB extends A
Is there a case where a companion object (singleton) for a class is needed?
Short question: How do I get the object.GetHashCode() value for an object that has
I've written a Scala trait, named Cache[A,B], to provide a caching API. The Cache
Possible Duplicate: What is the rationale behind having companion objects in Scala? Thanks for
Is there a way to do a comparison on object type for a trigger?
We are creating a WCF service with a companion client DLL (.Net) that we

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.