# Private constructors

**URL:** <https://users.scala-lang.org/t/private-constructors/4446>\
**Category:** Question\
**Created:** [May 7, 2019, 8:30am UTC](https://users.scala-lang.org/t/private-constructors/4446 "2019-05-07T08:30:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stairs](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@stairs](https://users.scala-lang.org/u/stairs)\
**Post date:** [May 7, 2019, 8:30am UTC](https://users.scala-lang.org/t/private-constructors/4446/1 "2019-05-07T08:30:27Z")

</div>

I have a

```
class E private (aList: List[T])

object E {
    def apply(something: T) = new E(List(something))
}

object MyTest {
  def main(args: Array[String]) {
     val myTobj = new T(...)
     println(new E(myTobj))
  }}

```

I am confused now because although in the code above I forgot to make the E object through the apply method of the companion object, it still compiles and works.

I removed the ‘new’ command in the println statement i.e. `println(E(myTobj))` and it does the same thing.

Did I build the object using the apply method and the new command was ignored or did the privateness of E’s constructor not get enforced?

There is a post [here](https://users.scala-lang.org/t/ending-the-confusion-of-private-case-class-constructor-in-scala-2-13-or-2-14/2915/12) about case class constructors but this is not a case class.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [May 7, 2019, 9:37am UTC](https://users.scala-lang.org/t/private-constructors/4446/2 "2019-05-07T09:37:22Z")

</div>

I get

```auto
error: constructor E in class E cannot be accessed in object MyTest

```

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [May 7, 2019, 10:12am UTC](https://users.scala-lang.org/t/private-constructors/4446/3 "2019-05-07T10:12:10Z")

</div>

I failed to reproduce that here: [https://scastie.scala-lang.org/MQuQR0Z3R2yQ95lCWF89Hg](https://scastie.scala-lang.org/MQuQR0Z3R2yQ95lCWF89Hg)

Can you see if you can reproduce?

---

<div class="post-metadata">

**Author:** ![stairs](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@stairs](https://users.scala-lang.org/u/stairs)\
**Post date:** [May 7, 2019, 10:13am UTC](https://users.scala-lang.org/t/private-constructors/4446/4 "2019-05-07T10:13:58Z")

</div>

I too get that message in the REPL which is confusing. I have done sbt clean a number of times to make sure its not working from old sources but no error is thrown.
