Geek
Geek

Reputation: 27203

How many instances of Class literals can be there residing in the JVM?

I have never specifically worked with class literals . I think they are mostly used in reflection apis . I want to understand how many instances can be there for class literals ? Are they instance controlled (for example Singletons?)

Class<String> stringClass = String.class;

Upvotes: 0

Views: 511

Answers (2)

Amit
Amit

Reputation: 817

Class is loaded as Object in Heap by Class Loader.

So only one class literal instance exists per class loader.

However you can create as many instances of type class in your code.

Upvotes: 0

Hot Licks
Hot Licks

Reputation: 47739

The Class object is (at least in theory) created when the class is loaded. There is only one per class. It's not correct to refer to it as a singleton, though, since there are multiple Class objects, each for a given class, just as you can have multiple String objects with different values.

Note that you can have two different Class objects named "a.b.c", if they are loaded by different class loaders. These would be two distinct classes (which may not even be similar) and two distinct Class objects.

Upvotes: 1

Related Questions