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 9184759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:07:54+00:00 2026-06-17T19:07:54+00:00

I have some objects that implement a trait. I am trying to have a

  • 0

I have some objects that implement a trait. I am trying to have a val that is static and common to all of these objects. I have read that the way to do this is to use a companion object for the trait. I have used the following:

trait Test

object Test extends Test{
  val a = 1
}

object Test2 extends Test{
  def test = {
    val b = a
  }
}

However, in the line val b = a I get a “not found: value a” error. I would appreciate some help on how to fix this.

  • 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-17T19:07:56+00:00Added an answer on June 17, 2026 at 7:07 pm

    Members of companion objects are by default not visible to anyone, even their companion classes. So you need an explicit import:

    trait Test
    
    object Test extends Test{
      val a = 1
    }
    
    object Test2 extends Test{
      import Test._
    
      def test = {
        val b = a
      }
    }
    

    I don’t know if there is a nice way to do it without an import in every subclass…

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

Sidebar

Related Questions

Let's say I have some model objects that resemble this: public class FooModel {
Assume some domain and view objects (that have no common base class) public class
I have a requirement to create some objects that implement a given interface, where
I have some classes that all implement a certain interface. I want to store
I have several templated objects that all implement the same interface: I.E. MyObject<datatype1> obj1;
I have a class that creates several IDisposable objects, all of these objects are
I have a wrapper class around some objects that I want to use as
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have some code that uses the SQL Server 2005 SMO objects to backup
I have applications(WinForm) that gets some objects from webservice. After receiving array I transform

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.