3 ms·
But can you pass classes around as objects after they're loaded? Do they have a value beyond their identifier?
by blasdel 17y ago
But can you pass classes around as objects after they're loaded? Do they have a value beyond their identifier?
- dminor 17y agoYes, although Java's reflection is somewhat of a pain in the ass and limited in many ways. But you can take a Class object and call newInstance(), and it will invoke the class's no-arg constructor.
- duskwuff 17y agoMoreover, reflection is less of a language feature and more of a very low-level library feature. In ObjC, one can write Class c = [SomeRandomClass class]; id thingy = [[c alloc] init]; but Java won't let you do the equivalent: Class c = SomeRandomClass; Object x = new c;
- pvg 17y agoYep, you can pass them around, they do have values, in fact, they can have different values for the same type sometimes. This has been the case since well before Java even had reflection - having them around is fairly central to the Java dynamic loading and security model.
- lucifer 17y agoWhen you define a class, say "Foo", instances of Foo are Foo objects. "Foo", itself, is an instance of a class named "Class" which is a direct descendent of the class "Object". In Java's type system, classes are 'first class objects': http://en.wikipedia.org/wiki/First-class_object http://en.wikipedia.org/wiki/First-class_object