Meaning of .this and .class in java

前端 未结 2 794
情歌与酒
情歌与酒 2020-11-28 22:41

Let\'s say we have a class name Home. What is the difference between Home.this and Home.class? What do they refer to?

2条回答
  •  鱼传尺愫
    2020-11-28 23:40

    Home.class returns the instance of java.lang.Class that corresponds to the class Home. This object allows you to reflect over the class (find out which methods and variables it has, what its parent class is etc.) and to create instances of the class.

    Home.this is only meaningful if you're inside a nested class of Home. Here Home.this will return the object of class Home that the object of the nested class is nested in.

提交回复
热议问题