A
A
Alexey24112020-07-20 19:52:08
Java
Alexey2411, 2020-07-20 19:52:08

.class/getClass()/Class.forName() in Java. How does it work?

I read that Class.forName() in java is needed to load a class into memory (including initialization of static blocks). Now it became interesting to me, if I use the .class literal in the code, will the class also be loaded into memory? If so, then what is the difference between Class.forName() and .class/.getClass() if they all give us a reference to an object of type Class that stores information about the class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-20
@Aleksey2411

The difference is that it Class.forName()can, by name, load a class that is not known at compile time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question