A classOf[T] is a value of type Class[T] . In other words, classOf[T]: Class[T] . For example: scala> val strClass = classOf[String] strClass: Class[String] = class java. lang. String scala> :t strClass Class[String]
It's just the name of the class.
In Scala, an object is a named instance with members such as fields and methods. An object and a class that have the same name and which are defined in the same source file are known as companions.
The Classname. this syntax is used to refer to an outer class instance when you are using nested classes; see Using "this" with class name for more details. However this. Classname is a compilation error ... unless you have declared an instance (or static) field with the name Classname .
There is a method classOf in scala.Predef
that retrieves the runtime representation of a class type.
val stringClass = classOf[String]
You can use the getClass
method to get the class object of an instance at runtime in the same manner as Java
scala> val s = "hello world"
s: String = hello world
scala> s.getClass
res0: Class[_ <: String] = class java.lang.String
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With