mizech mizech - 1 month ago 7
Java Question

What is a "runtime class" in Java?

I try to understand what the Object.getClass() method does.

The documentation says that it "returns the runtime class of an object":
https://www.tutorialspoint.com/java/lang/object_getclass.htm

The following explanation doesn't help me understanding the term.

Has someone a simple description of what a "runtime class" is and what .getClass does?

Answer

Just understand it as "an object that has all the metadata of the object's type". In that object, you can find the methods declared in the class, the fields, the type hierarchy, etc. This information will be typically used by code that uses reflection to either inspect objects/types or to run method without the need to have the class defined and compiled when they, themselves are being coded.

"Runtime" may be emphasized because the class definition may change over time, or the object may be declared as a supertype while it actually is an instance of a subtype of the one declared. When a certain class is loaded, it's that information, as loaded during that instance, that will be returned by the getClass() method.

In short, when your code runs, the VM will have a definition of your class in a different way than the "source" form that you type in a .java file. That information, of course after being compiled, will be loaded and all the metadata (as said above) will constitute what they call the "runtime class". It's just a fancy way to say "an object with all the metadata about a class loaded when the program is running"