Search code examples
scalamavencompanion-object

How to invoke class constructor, if class and object is in the same file


In MyFactory.scala, object and Class defined in the same file with same name, like this

package com.mydomain.app.module

object MyFactory{
    val a1 = "a1"
    val b1 = "b1"
}

class MyFactory(config:Configuration){
    //blah....
}

Problem is I cannot Initiate MyFactory object in another class

var myFactory = new Myfactory(defaultConfiguration)

due to the error

not found: type MyFactory

All I did was a common import

import com.mydomain.app.module.MyFactory

What is the valid way to initiate an object of the class, if I can't modify anything from MyFactory.scala (legacy code)


Solution

  • var myFactory = new MyFactory(defaultConfiguration)
    

    is the valid way to initiate an object of the class.

    import com.mydomain.app.module.MyFactory should be enough for bringing MyFactory (and its companion) to the scope.

    Sometimes "object app is not a member of package com.mydomain" can mean that you're trying to recompile MyFactory.scala referring to something not compiled in com.mydomain...

    Try mvn clean compile.