Why is this possible in Java: this getClass(). getClass(). Getclass()… Etc

Why is this possible in Java:

this.getClass().getClass().getClass().getClass()...

Why is there such infinite recursion?

Just curious.

Solution

Class object is still an object. You can call getClass on any object, thanks to the existence of object #getclass So you get:

this.getClass(); // Class<YourClass>
this.getClass().getClass(); // Class<Class<YourClass>>
this.getClass().getClass().getClass(); //Class<Class<Class<YourClass>>>

Eventually, for such a large program, you will run out of stack memory, time or disk space, or reach the internal limit of Java

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>