Let’s say we have a class name Home. What is the difference between Home.this and Home.class? What do they refer to?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Home.this
Home.thisrefers to the current instance of theHomeclass.The formal term for this expression appears to be the Qualified this, as referenced in Section 15.8.4 of the Java Language Specification.
In a simple class, saying
Home.thisandthiswill be equivalent. This expression is only used in cases where there is an inner class, and one needs to refer to the enclosing class.For example:
Home.class
Home.classwill return the representation of theHomeclass as aClassobject.The formal term for this expression is the class literal, as referenced in Section 15.8.2 of the Java Language Specification.
In most cases, this expression is used when one is using reflection, and needs a way to refer to the class itself rather than an instance of the class.