Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the get the classOf for a scala object type

Tags:

scala

I wonder how to get a class object for an object type in Scala. Ok, that is a mouth full because of the double meaning for object. So here an example which will fail:

object Main {    private [this] val TAG = classOf [Main].getName; } // Main 

If Main was class it works perfectly. Any ideas?

like image 899
Martin Avatar asked Mar 09 '12 14:03

Martin


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.

How do you find the datatype of a variable in Scala?

Use the getClass Method in Scala The getClass method in Scala is used to get the class of the Scala object. We can use this method to get the type of a variable.

How do I find the class name of an object?

If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.


1 Answers

scala> Main.getClass res1: java.lang.Class[_] = class Main$ 
like image 120
Alexey Romanov Avatar answered Oct 01 '22 04:10

Alexey Romanov