Java Reflection

There once was a guy with a bean
As for use, it remained to be seen
With a shout, this guy cried,
"I can look down inside
and see proteins and other cool things!"
(Umm... Anonymous)


Whatever anonymous fool wrote those lines was trying to get across the point that one cannot actually look inside a bean and see anything that looks remotely like anything except a bean. Just a bean. Sure, we know that there are proteins somewhere in there, maybe some other stuff that you can read about in any high school biology textbook, but for the most part you can't see anything inside a bean. Unless you're a scientist with special equipment, of course.

Too bad the bean couldn't just tell us what it had inside it. Better yet, if all food could just talk, we wouldn't need to have nutritional labels. Just ask the food itself.

So let's get back to Java. The Java 1.1 specification introduced many nice new features, many with nice new big words that are sometimes thrown around and relegated to special chapters in revisions of best-selling Java books. One of those new features is one we'll be discussing here, called reflection. Reflection is not difficult to understand, and quite useful. You may seldom use it directly, but it brings a host of new capabilities that, by the end of this lesson, you'll agree are invaluable even in day-to-day programming.

Here's what's expected of you before you begin this lesson: first, you should know something about XML, because we're going to use reflection to create a very useful class that writes Java objects to XML automatically. Although XML is relatively new as of May 19, 1998, in a few short months it will be more widely known and understood than HTML (remember, you heard it here first.) Nevertheless, I'll provide a few short paragraphs on XML later on.

Secondly, you should be at least relatively familiar with the Java language itself. Exeperience with similar programming languages, such as C++, would also be beneficial.

What is Reflection?

Reflection is Java's new (version 1.1) ability to look inside a Java object at runtime and see what variables it contains, what methods it supports, what interfaces it implements, what classes it extends -- basically everything about the object that you would know at compile time.

Now, since C++ was here first, let's use a quick example in that language to show you the problem and how Java handles it. Let's suppose we have a simple C++ class like the following:

class CMyClass
  int x;
  void setX(int newX) {x=newX;}
  int getX() {return x;}

OK, so that's not so different from a Java class. In fact, it's almost exactly the same. But let's assume for a moment that it's a C++ class—it's pretty simple to see what variables it has and what methods it provides.

What happens when it's compiled, however? Since we want to reuse this class (by the way, we want to reuse this class), we put it in an object file (myclass.obj), or better yet, a library file (myclass.lib). It's not as easy to tell what …