How do you understand the class parameter in scala. Will the compiler add implicit field to the class for the class parameter.
Like
class Rational(n:Int, d:Int){
val num = n
val den = d
override def toString = n + "/" + d
}
do we have 4 fields in class Rational (n, d, num, den)? If not, how can we access n and d in method toString. What’s the mechanism behind this?
nanddwill becomeprivatefields unless they are only used in the constructor, in which case they’re optimised away.numanddenwill also beprivatefields, but they getpublicaccessors as well.Try this:
Then compile and run
javap -private Test, which shows all classes and members of the class:Then try with a method that uses that parameter: