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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:13:15+00:00 2026-05-22T12:13:15+00:00

Greetings, How can I make the Foo constructor visible only to this package (unit

  • 0

Greetings,

How can I make the Foo constructor visible only to this package (unit test + companion object) ?

I don’t want to be able to instantiate Foo outside of this 2 files…

Foo.scala

package project.foo

class Foo(val value: String)

object Foo {
  def generate: Foo = new Foo("test")
}

FooSpec.scala

package project.foo

import org.spec2.mutable._

class FooSpec extends Specification {
  "Foo" should {
    "be constructed with a string" {
      val foo = new Foo("test")
      foo.value must be "test"
    }
  }
}

I’m using Scala 2.9

  • 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-22T12:13:16+00:00Added an answer on May 22, 2026 at 12:13 pm

    Try this:

    package project.foo
    class Foo private[foo] (value: String)
    

    Then the constructor of Foo is only accessible from the foo package.

    You can read more about Scala’s visibility (look especially for scoped private and scoped protected) here.

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

Sidebar

Related Questions

Greetings, I want to write a unit test to make sure that our web
Greetings, Can someone help me with this? I am trying to make a desktop
Greetings, I have a particular object which can be constructed from a file, as
Greetings. How can I make access to my article or post by their name?
Greetings, We can edit image attributes with this get_image_tag filter when we are adding
Greetings, I can't believe I'm asking such a basic question, but it doesn't make
Greetings everyone Using the request object, I can't get a sole value as in
Greetings, Is there was test or a predicate I can use in prolog to
Greetings, I feel kinda stupid for asking this, but I want to set the
Greetings, The VBA code below will create an Excel QueryTable object and display it

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.