While declaring a class in Scala, we can pass class parameters which are then used by compiler to generate a primary constructor:
class Rational(n:Int,d:Int){
override def toString = n + "/"+ d
}
//companion object
object Rational{
def main(args: Array[String]): Unit = {
val r = new Rational(1,2)
println(r.toString)
println(r.n)
}
}
In toString method, I can access n and d as class members. But when I try to access n from companion object, I get error as:
'Cannot resolve symbol n'
Edit: From link provided for possible answer I can understand the reason. But why is that I can access 'n' and 'd' in toString method?