Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala classOf vs getClass

It seems like I can call classOf[package.Class] on a class and package.Class.getClass() on an object, but not vice versa. Is there some syntax that will work on both a class and its companion object?

like image 425
Ben McCann Avatar asked Jul 10 '12 21:07

Ben McCann


People also ask

What is classOf in Scala?

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]

What is the type of an object in Scala?

Scala is more object-oriented than Java because in Scala, we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., Object. You create singleton using the keyword object instead of class keyword.

What is class and object in Scala?

Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.


1 Answers

A companion object is not related to the class. What are you trying to do? It might be that Scala 2.10.0 reflection will help, but types and instances are two different things in two different namespaces.

like image 184
Daniel C. Sobral Avatar answered Sep 30 '22 06:09

Daniel C. Sobral