Getting the Class Object and name of a Java Class and Interface

Sunday, February 8, 2009

I know the title might be little confusing, but its very simple, the question is how can I get the name of a java class or an interface in a String format? Here we will explore the different methods we can use to get the Class Object and name of a regular java class and a java interface.

Every regular java class has an inherited getClass method which returns the run time Class object of that java class, for java interfaces, the Class object is got by using the .class attribute of that interface, we can also use the forName method of a Class to get the Class Object and name of a regular java class or an interface, once you get the Class Object of a java class or an interface, you can use the getName method of that Class to get the name of the java Class Object in a String format, still confused, go through the below code on how to do the same in java for regular classes and interfaces and the differences between them.

Another question which may arise is why should anyone bother to get the Class object or name of a java class or an interface and where its used? There are many occasions where you need to pass the runtime Class object of a java class or an interface to a method, for example in MBean applications there are lots of methods which need the Class object of an MBean (interface) to be passed to get the corresponding MBean instance on which you can invoke its methods (Object Reflection), if you have never come across MBean, take it easy, here we are just going to discuss the different ways to get the Class Object and name of a java class and interface, see the code below.

Listing 1: ClassName.java

1: /**
2: * ClassName.java
3: * Author: S.Prasanna
4: */
5:
6: import javax.swing.text.html.HTML;
7: import java.lang.management.RuntimeMXBean;
8:
9: //Getting the Class Object and name of a java class and interface
10: public class ClassName {
11:
12: public static void main (String args[]) {
13:
14: // Getting the Class Object and name of a java class HTML
15:
16: HTML htmlObj = new HTML();
17: System.out.println("HTML Class Object using getClass method = "
18: + htmlObj.getClass());
19:
20: try {
21: System.out.println("HTML Class Object using Class.forName method = "
22: + Class.forName("javax.swing.text.html.HTML"));
23: } catch (Exception e) {
24: e.printStackTrace();
25: }
26:
27: System.out.println("HTML Class Object name using getName method = "
28: + htmlObj.getClass().getName());
29:
30: // Getting the Class Object and name of a java interface RuntimeMXBean
31:
32: System.out.println("RuntimeMXBean Class Object using RuntimeMXBean.class = "
33: + RuntimeMXBean.class);
34: try {
35: System.out.println("RuntimeMXBean Class Object using Class.forName method = "
36: + Class.forName("java.lang.management.RuntimeMXBean"));
37: } catch (Exception e) {
38: e.printStackTrace();
39: }
40:
41: System.out.println("RuntimeMXBean Class Object name using getName method = "
42: + RuntimeMXBean.class.getName());
43: }
44: }

Output:

HTML Class Object using getClass method = class javax.swing.text.html.HTML
HTML Class Object using Class.forName method = class javax.swing.text.html.HTML
HTML Class Object name using getName method = javax.swing.text.html.HTML
RuntimeMXBean Class Object using RuntimeMXBean.class = interface java.lang.management.RuntimeMXBean
RuntimeMXBean Class Object using Class.forName method = interface java.lang.management.RuntimeMXBean
RuntimeMXBean Class Object name using getName method = java.lang.management.RuntimeMXBean

Explanation:

The above code is quite simple, we are importing a class (line 6, javax.swing.text.html.HTML) and an interface (line 7, java.lang.management.RuntimeMXBean) and for the HTML class, we are getting its Class Object using its inherited getClass method (lines 17 - 18) or through the Class.forName method (lines 21 - 22, here we have to pass the fully qualified package and class name to the forName method). Then we are printing the Class name as a String using the Class Object's getName method (lines 27 - 28).

For java interfaces, getting its Class Object and the class name is little different, since an interface is not a regular class, we can't invoke a method like getClass to get the corresponding class object, therefore we need to use the .class attribute of the interface to get the corresponding class object (line 32 - 33) or we can use the Class.forName method to get the Class Object by passing the fully qualified package and class name as its argument (line 35 - 36), to get the Class name of the interface, we can use the getName method on the Class Object we got using .class attribute of that interface (lines 41 - 42).

In my next post, we will see how to get the Class Object and the name of a java class and interface using jython, you are in for some small surprises as things are little different in jython and I don't want to confuse you about that in this writing, especially if you are a core java programmer.

No comments:


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.