What is a “runtime class” in Java?

后端 未结 4 1181
故里飘歌
故里飘歌 2021-01-12 11:51

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

The documentation says that it \"returns the runtime class of an object.\" That explanation

相关标签:
4条回答
  • 2021-01-12 12:33

    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.

    0 讨论(0)
  • 2021-01-12 12:38

    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.
    
    0 讨论(0)
  • 2021-01-12 12:46

    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"

    0 讨论(0)
  • 2021-01-12 12:53

    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.

    0 讨论(0)
提交回复
热议问题