how do i obtain an instance of a class without spe...
# getting-started
s
how do i obtain an instance of a class without specifying it explicitly
Copy code
fun ret() {
    class a {
        var empty: Int = 0
    }

    val f = arrayListOf<a>()
    f.add(a()) // class a is explicitly specified
}
s
You can't. And that's all I'm going to say. You need to read the docs
s
if i do v.add(v::class.java as E) then i get a type of
class java.lang.Class
why ;-; isnt the JVM meant to be able to know what type of class X is?
i
Read about the type erasure in JVM generics. In run time
ArrayList<a>
doesn't know its element type, so you cannot obtain it with reflection.
Why do you need to instantiate class
a
without specifying its name?
Perhaps if you need to add something to a list and you don't have anything to add, just declare the list having nullable elements and add
null
there.