Join David Gassner for an in-depth discussion in this video Using the Class class, part of Advanced Java Programming.
The Java reflection API contains classes and methods that allow you to dynamically instantiate and interrogate classes. This allows you to build highly dynamic applications, loading classes to find, say, an XML or other kinds of text files or handling other kinds of processes where you don't know the names of classes when you're coding. I'll demonstrate the basics of the reflection API. The most important part of which is a class, which is simply named class. Each object that you create in a Java application has an associated class of which that object is an instance, and you can find out using TheClassClass, what that class name is and what its capabilities are.
I'm working in a project called TheClassClass, which has the main class with the main method, and then in Olive class and an OliveName enumerator class. I'll go back to Main.java, and I'll start by instantiating the olive class, I'll create the data type as Olive, I'll make sure that I have the import statement, and I'll assign my new object a variable name of simply o. I'll instantiate the object using this Olive class as only constructor method, which requires two arguments.
The first is a value of the OliveName Enum class, and I'll choose PICHOLINE as the value and then I'll pass in the second required argument a long value representing the color. Now I'll set that to a hex value representing green. Now I'll get a reference to the class of which this object is an instance, I'll type the data type of the object I'm about to create, and it'll be named simply class. I'll press Ctrl+Space and choose Class from java.lang, and that feels in the diamond operator. This syntax will be familiar from using collections such as ArrayList. Just like those collections, TheClassClass uses this generic notation to allow you to designate what the data type of the class is.
Typically though, when you're using this sort of notation the reason is because you don't know what the data type is exactly and so instead of putting in the class name you can put in a question mark, and that means it's a class of some data type, but I don't know what it is. I'll give this object an ID of c, and I'll get its reference from o.getClass. So now I have an object name c, which is datatyped as class, which represents the Olive class, and I know that because I created the object from which I got the reference. Got that? Okay, let's move forward.
Once you have the class you have enormous capabilities in finding out information about the class's name and its capabilities. Let's take a look at some documentation this is the Java API documentation for the Class Class, notice that's how it's notated up here. It's a class named class, and let's go to the methods that are available. Once you have a reference to the class object you can get all sorts of information from it. For example, let's find out dynamically what the name of this class is. I'll use sysout, and I'll output the class itself, I'll Save and Run the code, and I get the word class followed by the fully qualified package and class name.
Now, what if I want that string the package and class, but not the word class? Well, then I can use a method of the class called get name. Once again, I'll use sysout and press Ctrl+Space, and this time I'll output c.getName. I'll Save and Run again, and now I get the fully qualified package and class, and if all I want is the class name and not the full package, I can use a method called getSimpleName, I'll duplicate this line of code, go to the method call and get rid of everything after get press Ctrl+Space, and this time, I'll choose getSimpleName. I'll clean up the parentheses there and run the code, and now in the console, I get class and the fully qualified name that's when I passed the class object in itself.
The package and name that's when I use getName and then only the name, and that's the result of getSimpleName. So this is a quick look at some of the capabilities of TheClassClass. There's a lot more you can do with it, including getting information about constructors, methods, fields and other members of the class, and I'll go into some of those details in the next couple of videos.
- Installing Java on Windows and Mac OS X
- Installing Eclipse
- Using new features such as simplified generics
- Working with advanced class structures (member, local inner, etc.)
- Using the Reflection API
- Navigating inheritance trees
- Managing unordered and ordered sets
- Peeking and polling with queues
- Testing and error handling
- Managing files and directories
- Working with I/O streams
- Next steps with Java