A "Reflection" on Java
In 500 words or so, what can we say about reflection? It's not a subject you would ordinarily treat lightly, most experts would tell you. But it's important, and here at Java Jolt, we have a quixotic belief that if you haven't clicked on the ad banner yet, and we write creatively enough, nothing in all of computer science can seem all that complicated.If you're one of those stalwart souls who reads us every week, you've probably guessed by now that, since we're talking about it, it must be one of those nifty new features added to the new version of the Java Development Kit. If that's what you're thinking, you're right. So what's it for?
This is actually one of those times when the jargon-based name is not altogether lousy. Introspection, reflection, or whatever else you prefer to call it is there to provide a new degree of self-awareness to your Java code. And, since the entire foundation of computing stems from a certain degree of roughly this kind of self-awareness, it's no wonder experts warn that it's complicated, we find it significant, and the Java team didn't hesitate long to include it in their product. And did we mention, this very concept is probably what ended up misleading a number of computer scientists cum philosophers into thinking that computers would one day be able to think?
You're actually in deeper than you thought here. Although the casual applet programmer may never use it, there's nothing we've discussed thus far that has as much potential for tangled amateur philosophy as a computer's ability for introspection, metaphorical though it is.
For you, the Java enthusiast, all it means is that there are now better objects to represent your objects, and better classes to represent your classes. The language now contains a system for describing itself to itself, while it's running.
There's always been a class called
java.lang.Class, but in order to make Java more fully self-aware, it's been expanded significantly, and we've been given (among a larger supporting cast) Field, Method, and Constructor classes as well, all organized under another new slice of the API calledjava.lang.reflect.While a few of you who're probably so smart you don't need to be reading this anyway may be nodding your heads in understanding, most of you are probably wondering what the heck all this means. If you are, be reassured, it's not as bad as you think.
There are a few highly specific reasons for doing this: Maybe you want to write a better Java debugger (this really facilitates that), or perhaps you just want all your programs to be able to explain themselves, all the time (which could be good if you're trying to teach people about how they work). Most importantly, perhaps, there's a big practical implication. Now, when you're programming, you can take all those methods and classes whose names you've had to write out explicitly when you wanted to use them, for example,
System.out.println("Goodbye World!");
and treat them like variables too. Now rather than you, the programmer, having to decide on what methods to run and what classes to use in advance, you can program your program to decide, while it's running, in new and more powerful ways. One could ask for the Method object associated with
println, store it in a big array along with a bunch of other methods, and let there be a big complicated algorithm for deciding whether or not it gets picked (or perhaps it would pick one for display in an alert box instead, or in the bottom of your browser window, or--you get the idea).
[...our context-sensitive decision-making code...] displayRoutines[chosen_display].invoke(...);
We're still glossing over this a bit, of course. There are security considerations, details regarding how to call a Method's
invokemethod (does anyone else find this funny?), and other bits and pieces of technical miscellania. But the basic idea is that the program can now take a greater role in programming itself--hence, self-awareness, or "reflection." The nitty-gritty details we'll get to, along with a more illuminating example, in good time.The whole reason computers are possible has to do with a conceptually similar breakthrough in electrical engineering made not so long ago in this very century--called the relay. More recent incarnations include the vacuum tube and the infamous transistor.
Basically, while other people were busy designing circuits in which switches of one kind or another changed the path that the electricity took (and therefore the outcome of whatever the circuit was doing), someone came up with the bright idea that electricity could determine the path the electricity took. The idea (taking the transistor, for instance) is to let electricity flow, or not flow, through two connectors, depending on whether some other electricity is being applied to a third.
It's not really mechanically that much more complicated than a diode. Yet it had profound and still unknown repercussions for the whole world as we know it. These kinds of things led to electric logic gates and their ilk, and these technologies led right into the modern electronic computer, all in under half a century.
Introspection, then, is obviously key; it can certainly be extremely powerful if wielded correctly. It's this degree of self-awareness in computing machinery that led a few of the more licentious philosophers of our time to postulate that computers themselves were self-aware in some capacity--a rumor that we can neither confirm nor deny.
While the concepts behind introspection may be simple, applications involving them can quickly become confoundingly complicated to the average mortal. Again, as evidence, there's the modern computer. Things are never nearly as orderly when you introduce the ability for your systems to modify themselves.
Perhaps confusingly, we've mentioned in previous weeks that this is a spin-off from other productions in the Java API, which is partially true. Aside from standing on its own merits, making this a part of the Java language was immediately recommended by the need to make JavaBeans work well. Yet this capability can arguably be considered important to any high-level language, and many have long possessed it; C, for instance, has had it for decades, since its inception.
At any rate, introspection is now part of the Java world, and a welcome addition at that. We may now look forward to vastly more confusing, and equally more powerful, Java applications. When we've finished our lengthy take on the current bit of Java's evolution, we'll undoubtedly return to treat it more thoroughly.
In the meantime, check out the documentation, and come back next week for another bite out of the new Java.