Reputation: 361
When I am declaring my outer class object as a var
, I am not able to instantiate the inner class. But when I am making it as a val
, I am not getting any error. Why is this happening?
class Outer(name : String ) { ter =>
class Iner(name : Inner) {
println("Printing outer class name : " + ter.name )
println("Printing inner class name : " + name )
}
}
object OverRiding extends App {
var outr : Outer = new Outer("Priyaranjan Outer")
var inner = new outr.Iner("Priyaranjan Inner")
}
Upvotes: 2
Views: 80
Reputation: 42047
The problem is that in a constructor invocation, the expression referring to the class getting instantiated must be a stable identifier. That is required in the language specification at http://www.scala-lang.org/files/archive/spec/2.11/05-classes-and-objects.html#constructor-invocations
If you look at the definition of a stable identifier, you will see why outr
has to be a val
:
- p.x where p is a path and x is a stable member of p. Stable members are packages or members introduced by object definitions or by value definitions of non-volatile types.
Upvotes: 3