Take the 2-minute tour ×

The fact that you can do the following in Scala is pretty neat:

scala> class FooBar
defined class FooBar

scala> val a = new FooBar
a: FooBar = FooBar@7efeedca

scala> val the_class = a.getClass
the_class: java.lang.Class[_ <: FooBar] = class FooBar

scala> val b = the_class.newInstance
b: FooBar = FooBar@1ef1df56

Suppose I want to set the value of the_class directly. I appear to be able to declare a variable of the correct type:

scala> var the_class: java.lang.Class[_ <: FooBar] = null
the_class: java.lang.Class[_ <: FooBar] = null

But I don't appear to be able to bind the variable to any value. Is this possible?

scala> the_class = class FooBar
<console>:1: error: illegal start of simple expression
       the_class = class FooBar
scala> the_class = FooBar
<console>:9: error: not found: value FooBar
       the_class = FooBar
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Did you mean:

val the_class = classOf[FooBar]
share|improve this answer
Yes! Don't know how I missed this. Thanks. –  Dan Barowy Mar 7 '12 at 20:54
add comment

Your Answer


