{"id":83447,"date":"2020-09-27T14:54:22","date_gmt":"2020-09-27T14:54:22","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=87754"},"modified":"2020-09-27T14:54:22","modified_gmt":"2020-09-27T14:54:22","slug":"the-difference-between-a-getclass-and-a-class-in-java","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/09\/27\/the-difference-between-a-getclass-and-a-class-in-java\/","title":{"rendered":"The Difference Between a.getClass() and A.class in Java"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"overview\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview\"><\/div>\n<p>In Java, <strong>the class <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Class.html\"><em>java.lang.Class<\/em><\/a> is the entry point of all reflection operations<\/strong>. Once we have an object of <em>java.lang.Class<\/em>, we can then call the corresponding methods to get the objects of the reflection classes.<\/p>\n<p>In this tutorial, we&#8217;ll discuss the differences between two different ways to get an object of <em>java.lang.Class<\/em>:<\/p>\n<ul>\n<li>Calling the <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Object.html#getClass--\">Object.getClass()<\/a><\/em> method<\/li>\n<li>Using the <em>.class<\/em> syntax<\/li>\n<\/ul>\n<h2 data-id=\"short-introduction-to-the-two-approaches\">2. Short Introduction to the Two Approaches<\/h2>\n<div class=\"bd-anchor\" id=\"short-introduction-to-the-two-approaches\"><\/div>\n<p>The\u00a0<em>Object.getClass()<\/em> method is an instance method of the\u00a0<em>Object<\/em> class. If we have an object, we can call <em>object.getClass()<\/em> to get the <em>Class<\/em> object of its type.<\/p>\n<p>Similarly, we can use the <em>ClassName.class<\/em> syntax to get the <em>Class<\/em> object of the type. An example can explain it clearly:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void givenObjectAndType_whenGettingClassObject_thenTwoMethodsHaveTheSameResult() {\r\n    String str = \"I am an object of the String class\";\r\n    Class fromStrObject = str.getClass();\r\n    Class clazz = String.class;\r\n    assertSame(fromStrObject, clazz);\r\n}\r\n<\/code><\/pre>\n<p>In the test method above, we try to obtain the\u00a0<em>Class\u00a0<\/em>object of the\u00a0<em>String<\/em> class using the two ways we&#8217;ve mentioned. Finally, the assertion method tells us that the two <em>Class<\/em> objects are the same instance.<\/p>\n<p>However, there are differences between the two approaches. Let&#8217;s take a closer look at them.<\/p>\n<h2 data-id=\"the-runtime-type-vs-the-static-type\">3. The Runtime Type vs. the Static Type<\/h2>\n<div class=\"bd-anchor\" id=\"the-runtime-type-vs-the-static-type\"><\/div>\n<p>Let&#8217;s review the previous example quickly. <strong>When we call the <em>str.getClass()<\/em> method, we get the runtime type of the <em>str<\/em> object. On the other hand, <em>String.class <\/em>evaluates the\u00a0<em>String<\/em> class statically<\/strong>. In this example, the runtime type of\u00a0<em>str<\/em> and the\u00a0<em>String.class<\/em> are the same.<\/p>\n<p>However, they can be different if class inheritance joins the party. Let&#8217;s see two simple classes:<\/p>\n<pre><code class=\"language-java\">public class Animal {\r\n    protected int numberOfEyes;\r\n}\r\npublic class Monkey extends Animal {\r\n    \/\/ monkey stuff\r\n}<\/code><\/pre>\n<p>Now let&#8217;s instantiate an object of the <em>Animal<\/em> class and do another test:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void givenClassInheritance_whenGettingRuntimeTypeAndStaticType_thenGetDifferentResult() {\r\n    Animal animal = new Monkey();\r\n    Class runtimeType = animal.getClass();\r\n    Class staticType = Animal.class;\r\n    assertSame(staticType, runtimeType);\r\n}\r\n<\/code><\/pre>\n<p>If we run the test above, we&#8217;ll get a test failure:<\/p>\n<pre><code class=\"language-bash\">java.lang.AssertionError: ....\r\nExpected :class com.baeldung.getclassobject.Animal\r\nActual   :class com.baeldung.getclassobject.Monkey<\/code><\/pre>\n<p>In the test method, even if we instantiated the\u00a0<em>animal<\/em> object by <em>Animal animal = new Monkey();<\/em> instead of <em>Monkey animal = new Monkey();<\/em>, the runtime type of the\u00a0<em>animal<\/em> object is still\u00a0<em>Monkey.\u00a0<\/em>This is because the\u00a0<em>animal<\/em> object is an instance of\u00a0<em>Monkey<\/em> at runtime.<\/p>\n<p>However, when we get the static type of the\u00a0<em>Animal<\/em> class, the type is always\u00a0<em>Animal<\/em>.<\/p>\n<h2 data-id=\"handling-primitive-types\">4. Handling Primitive Types<\/h2>\n<div class=\"bd-anchor\" id=\"handling-primitive-types\"><\/div>\n<p>When we write Java code, we use primitive types quite often. Let&#8217;s try to get a <em>Class<\/em> object of a primitive type using the\u00a0<em>object.getClass()<\/em> approach:<\/p>\n<pre><code class=\"language-java\">int number = 7;\r\nClass numberClass = number.getClass();<\/code><\/pre>\n<p>If we try to compile the code above, we&#8217;ll get a compilation error:<\/p>\n<pre><code class=\"language-bash\">Error: java: int cannot be dereferenced<\/code><\/pre>\n<p>The compiler can&#8217;t dereference the <em>number<\/em> variable since it is a primitive variable. Therefore, <strong>the <em>object.getClass()<\/em> method can&#8217;t help us to get the <em>Class<\/em> object of a primitive type.<\/strong><\/p>\n<p>Let&#8217;s see if we can get the primitive type using the <em>.class<\/em> syntax:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void givenPrimitiveType_whenGettingClassObject_thenOnlyStaticTypeWorks() {\r\n    Class intType = int.class;\r\n    assertNotNull(intType);\r\n    assertEquals(\"int\", intType.getName());\r\n    assertTrue(intType.isPrimitive());\r\n}\r\n<\/code><\/pre>\n<p>So, we can obtain the <em>Class<\/em> object of the <em>int <\/em>primitive type through\u00a0<em>int.class<\/em>.\u00a0 In Java version 9 and later, a <em>Class<\/em> object of primitive type belongs to the <em>java.base <\/em>module.<\/p>\n<p>As the test shows, <strong>the <em>.class<\/em> syntax is an easy way to get the <em>Class<\/em> object of a primitive type.<\/strong><\/p>\n<h2 data-id=\"getting-the-class-without-an-instance\">5. Getting the Class Without an Instance<\/h2>\n<div class=\"bd-anchor\" id=\"getting-the-class-without-an-instance\"><\/div>\n<p>We&#8217;ve learned that the <em>object.getClass()<\/em> method can give us the <em>Class<\/em> object of its runtime type.<\/p>\n<p>Now, let&#8217;s consider the case where we want to obtain a <em>Class<\/em> object of a type, but we can&#8217;t get an instance of the target type because it&#8217;s an <em>abstract<\/em> class, an <em>interface,\u00a0<\/em>or some class doesn&#8217;t allow instantiation:<\/p>\n<pre><code class=\"language-java\">public abstract class SomeAbstractClass {\r\n    \/\/ ...\r\n}\r\ninterface SomeInterface {\r\n   \/\/ some methods ...\r\n}\r\npublic class SomeUtils {\r\n    private SomeUtils() {\r\n        throw new RuntimeException(\"This Util class is not allowed to be instantiated!\");\r\n    }\r\n    \/\/ some public static methods...\r\n}\r\n<\/code><\/pre>\n<p>In these cases, we can&#8217;t get the <em>Class<\/em> objects of those types using the\u00a0<em>object.getClass()<\/em> method, but <strong>we can still use the <em>.class<\/em> syntax to obtain the\u00a0<em>Class<\/em> objects<\/strong> of them:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void givenTypeCannotInstantiate_whenGetTypeStatically_thenGetTypesSuccefully() {\r\n    Class interfaceType = SomeInterface.class;\r\n    Class abstractClassType = SomeAbstractClass.class;\r\n    Class utilClassType = SomeUtils.class;\r\n    assertNotNull(interfaceType);\r\n    assertTrue(interfaceType.isInterface());\r\n    assertEquals(\"SomeInterface\", interfaceType.getSimpleName());\r\n    assertNotNull(abstractClassType);\r\n    assertEquals(\"SomeAbstractClass\", abstractClassType.getSimpleName());\r\n    assertNotNull(utilClassType);\r\n    assertEquals(\"SomeUtils\", utilClassType.getSimpleName());\r\n}\r\n<\/code><\/pre>\n<p>As the test above shows, the\u00a0<em>.class<\/em> syntax can obtain the <em>Class<\/em> objects for those types.<\/p>\n<p>Therefore, <strong>when we want to have the\u00a0<em>Class<\/em> object, but we cannot get an instance of the type, the <em>.class<\/em> syntax is the way to go.<\/strong><\/p>\n<h2 data-id=\"conclusion-1\">6. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion-1\"><\/div>\n<p>In this article, we learned two different ways to get the <em>Class<\/em> object of a type: the <em>object.getClass()<\/em> method and the <em>.class<\/em> syntax.<\/p>\n<p>Later, we discussed the difference between the two approaches. The following table can give us a clear overview:<\/p>\n<table style=\"border-collapse: collapse;height: 203px\" border=\"1px\" width=\"951\">\n<tbody>\n<tr>\n<td><\/td>\n<th><em>object.getClass()<\/em><\/th>\n<th><em>SomeClass.class<\/em><\/th>\n<\/tr>\n<tr>\n<td align=\"center\"><strong>Class objects<\/strong><\/td>\n<td>The runtime type of <em>object<\/em><\/td>\n<td>The static Type of\u00a0<em>SomeClass<\/em><\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><strong>Primitive types<\/strong><\/td>\n<td align=\"center\">&#8212;<\/td>\n<td>Works straightforwardly<\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><strong>Interfaces, abstract classes, or classes that can&#8217;t be instantiated<\/strong><\/td>\n<td align=\"center\">&#8212;<\/td>\n<td>Works straightforwardly<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>As always, the full source code of the article is available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/core-java-modules\/core-java-lang-3\">over on GitHub<\/a>.<\/p>\n<p>The post <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-getclass-vs-class\/\" >The Difference Between a.getClass() and A.class in Java<\/a> first appeared on <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/\" >Baeldung<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/636031156\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/636031156\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-getclass-vs-class#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-getclass-vs-class\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn the differences between calling the Object.getClass() method and using the .class syntax<\/p>\n<p>The post <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/feeds.feedblitz.com\/~\/636031156\/0\/baeldung~The-Difference-Between-agetClass-and-Aclass-in-Java\/\" target=\"_blank\">The Difference Between a.getClass() and A.class in Java<\/a> first appeared on <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/www.baeldung.com\/\" target=\"_blank\">Baeldung<\/a>.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/636031156\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/636031156\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-getclass-vs-class#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-getclass-vs-class\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":915,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"The Difference Between a.getClass() and A.class in Java - ITTeacherITFreelance.hk","description":"Learn the differences between calling the Object.getClass() method and using the .class syntax The post The Difference Between a.getClass() and A.class in Java"},"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83447"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/915"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=83447"}],"version-history":[{"count":5,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83447\/revisions"}],"predecessor-version":[{"id":85414,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83447\/revisions\/85414"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=83447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=83447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=83447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}