{"id":88,"date":"2006-05-08T22:49:03","date_gmt":"2006-05-09T03:49:03","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/05\/08\/javaclass\/"},"modified":"2006-05-08T22:49:03","modified_gmt":"2006-05-09T03:49:03","slug":"javaclass","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/05\/08\/javaclass\/","title":{"rendered":"Java.class"},"content":{"rendered":"<p>In Java, given an object, an instance of a class, you can get the class name by coding the following:<\/p>\n<pre>\nClass clazz = obj.getClass();\nString clazzName = clazz.getName();\n<\/pre>\n<p>Sometimes you want you want to create a Class object for a given class.  In this case you can do so by writing code similar to the following example:<\/p>\n<pre>\nClass clazz = MyClass.class;\n<\/pre>\n<p>Many plugin frameworks, including the JDBC Driver Manager will create a Class object without having the knowledge of what class name at compile time.  There might be a case where you know a class implements a given interface but you don&#8217;t know the class name of the implementation until at runtime when you read it from a properties file.  In situations like this you can do the following:<\/p>\n<pre>\nString clazzName = \"com.juixe.techknow.MyClass\";\n...\nClass clazz = Class.forName(clazzName);\n<\/pre>\n<p>Sometimes you will need a Class object for a primitive type.  You might need a Class object for an int or boolean when dealing with reflection.  In this case you do so using the dot class notation on a primitive type.  Here is a more elaborate example where we create a Class object for an int primitive:<\/p>\n<pre>\nint newValue = ...\nClass clazz = obj.getClass();\nMethod meth = clazz.getMethod(\"setValue\", new Class[]{int.class});\nmeth.invoke(obj, new Object[]{new Integer(newValue)});\n<\/pre>\n<p>You can also get the class for an array.  The only place where I have ever need the class of an array is when working with reflection.  Here is how you can get the class for an array:<\/p>\n<pre>\nClass clazz = String[].class;\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/java\" rel=\"tag\">java<\/a>, <a href=\"http:\/\/technorati.com\/tag\/class\" rel=\"tag\"> class<\/a>, <a href=\"http:\/\/technorati.com\/tag\/object\" rel=\"tag\"> object<\/a>, <a href=\"http:\/\/technorati.com\/tag\/reflection\" rel=\"tag\"> reflection<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, given an object, an instance of a class, you can get the class name by coding the following: Class clazz = obj.getClass(); String clazzName = clazz.getName(); Sometimes you want you want to create a Class object for a given class. In this case you can do so by writing code similar to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-1q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}