Why in scala we need to define class structure to create new object of the same class -


class readhelper{} object readhelper {}  class mainapp{    val rhelp = readhelper//this line uneasy if omit class declaration of readhelper } 

why in scala need define class structure create new object of same class?

actually don't.

class readhelper{}  class mainapp{   val rhelp: readhelper = new readhelper } 

please pay attention in original case

class readhelper {} object readhelper {}  class mainapp{   val rhelp: readhelper.type = readhelper } 

or just

object readhelper {}  class mainapp{   val rhelp: readhelper.type = readhelper } 

rhelp has different type.

object readhelper not object (aka instance in oop-languages) of class readhelper. it's called companion object of class. class-like structure (a singleton) existing besides class. in byte code find 2 classes readhelper (the class itself) , readhelper$ (companion object).

maybe should read more classes, objects, companion objects in scala.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -