What is the Scala equivalent of Java's ClassName.class?

Scala

Scala Problem Overview


How do I get an instance of Class in Scala? In Java, I can do this:

Class<String> stringClass = String.class;

What would be the equivalent in Scala?

Scala Solutions


Solution 1 - Scala

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel C. SobralView Question on Stackoverflow
Solution 1 - Scalaoxbow_lakesView Answer on Stackoverflow