10

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

The documentation says that it "returns the runtime class of an object." That explanation doesn't help me understanding the term.

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

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
cluster1
  • 4,968
  • 6
  • 32
  • 49

4 Answers4

8

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"

ernest_k
  • 44,416
  • 5
  • 53
  • 99
6

It means "the class of the instance the variable refers to at runtime" (sorry if that's not actually clearer).

If you have a reference to an Object, it could refer to an Object, a String, an Integer... you get that class, not Object.

Object obj1 = new Object();
System.out.println(obj1.getClass());  // java.lang.Object

String obj2 = "";
System.out.println(obj2.getClass());  // java.lang.String

obj1 = obj2;
System.out.println(obj1.getClass());  // java.lang.String, not Object.
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
4

Every class you write has a lot of metadata. That metadata consists of the class name, its fields, its methods, its base classes, the interfaces it implements, and so on.

Sometimes you may need to access that metadata from your code at runtime.

In order to do so, you can take any object and call its getClass() method. You will receive a Class object that will contain the above metadata.

Shloim
  • 5,281
  • 21
  • 36
  • And it will represent the runtime class, not the reference class. `Object o = new String()` => you will get String, not Object. – Fildor Nov 08 '16 at 12:20
2

The class of an object can change at runtime. Consider following example:

package demo;

public class Main {

    public static class A {
        public int a=0;
    }

    public static class B extends A {
        public int b=1;
    }

    public static void main(String[] args) {

        Main.A b=new Main.A();

        System.out.println(b.getClass().toString());

        b=new Main.B();

        System.out.println(b.getClass().toString());
    }   

}

The output of b.getClass() changed at runtime.

Matthias
  • 3,458
  • 4
  • 27
  • 46