简体   繁体   中英

Compile and run-time classes

Lets imagine I have a class called Person which is a generalization of another class Man . If I am to make a couple of instances of this class

Man man = new Man();
Person person = new Man();

Now, the compile-time class of the instance being referenced from the variable man is Man and the compile-time class of person is Person while the run-time class of both instances is Man . So far, im completely on board with the terminology because the instances which are created at run-time both are of class Man . But, if I where to cast the man instance as follows

Person personMan = (Person) man;

how come the run-time type of personMan is still Man? Is the run-time class of a instance only set when a new instance is created? Also, is there a way of actually getting the compile time class of a variable at runtime, so I could query what type of class personMan is (getClass would return Man ).

Edit: "compile-time class of a class" was a mistake (and doesn't make much sense). What I meant was variable (hence they question about what type of class personMan is :))

It's important to distinguish between three different concepts here:

  • Variables ( man , person )
  • References (the values of the variables)
  • Objects (the blobs of memory that the references, um, refer to)

The type of an object never changes after it's created. Casting a reference to a different type only affects the compile-time type of that expression. The result of a reference-type cast expression is always the same the original reference - it still refers to the same object, which still has the same type. (That's leaving boxing aside - and of course the cast can fail at execution time, leading to an exception.)

Also, is there a way of actually getting the compile time class of a class at runtime

If you mean the compile-time type of a variable - not if it's a local variable, without really deep inspection of the byte code. If it's a field you can use reflection to get at it. Why do you want to know?

The runtime type is the type of the new. new Man() is always Man, no matter the type of the variable where you store it.

The compile type is the type of the declared variable.

In your example

Person personMan = (Person) man;

you only can code with methods and attributes of personMan. You can also do

((Man)personMan).someManMethod();

but this can lead to a error if personMap hasn´t stored a instance of Man.

In Java, variables (other than primitives) are just references to objects. The objects themselves live off somewhere else and can never be directly accessed.

In every case, you have a Man object sitting somewhere, and the different references just give access to different subsets of the capabilities of that Man object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM