logical structure/details of a reference variable and object in the memory?

后端 未结 4 1302
囚心锁ツ
囚心锁ツ 2021-02-09 11:47

Let\'s say we have a class:

class Class1
{
   int i = 1;
}

and we have a variable:

Class1 ob1 = new Class1();
4条回答
  •  有刺的猬
    2021-02-09 12:13

    enter image description here

    This is one of the scheme in which the JVM can store the information of the class for the check at runtime using instanceOf.

    Every Java virtual machine must have the capability to determine information about its class, given only a reference to an object. This is needed for many reasons, including type-safe casting and the instanceof operator.

    This is one way in which a Java virtual machine implementation could associate class information with the instance data for an object. In this figure, a native pointer to a data structure containing class information is stored along with the instance variables for an object. The details in which the various ways a JVM could connect an object's data with its class information are beyond the scope of this article. The important thing to understand here is that class information will in some way be associated with the instance data of objects, and that the instance data includes fields for an object's class and all its superclasses.

    Artima post on Object initialization

    So when you do instanceOf check the information about the class is accessed via this pointer. But again do keep in mind that the exact implementation about the storage of class information may be implementation specific.

提交回复
热议问题