All the rar files have the same 
password : http://learning4you.blogspot.com/

Monday, November 10, 2008

Class and Object Reflection in Java

Class and Object Reflection (Java 1.1)

Reflection, also known as introspection, is a somewhat lofty term to describe the ability to "look inside" a class or an object and get information about that object's variables and methods as well as actually set and get the values of those variables and to call methods. Object reflection is useful for tools such as class browsers or debuggers, where getting at the information of an object on-the-fly allows you to explore what that object can do, or for component-based programs such as Java Beans, where the ability for one object to query another object about what it can do (and then ask it to do something) is useful to building larger applications.

The classes that support reflection of Java classes and objects will be part of the core Java 1.1 API (they are not available in the 1.0.2 version of the JDK). A new package, java.lang.reflect, will contain new classes to support reflection, which include the following:

  • Field, for managing and finding out information about class and instance variables
  • Method, for managing class and instance methods
  • Constructor, for managing the special methods for creating new instances of classes (you'll learn more about constructors on Day 7)
  • Array, for managing arrays
  • Modifier, for decoding modifier information about classes, variables and methods (more about modifiers on Day 15, "Modifiers, Access Control, and Class Design")
In addition, there will be a number of new methods available in the Class class to help tie together the various reflection classes.

You can find out more about the new reflection classes and methods from http://java.sun.com/products/JDK/1.1/designspecs/reflection/.

The Java Class Library

To finish up today, let's look at the Java class library. Actually, you've had some experience with some of the Java classes already, so they shouldn't seem that strange.

The Java class library provides the set of classes that are guaranteed to be available in any commercial Java environment (for example, in any Java development environment or in browsers such as Netscape). Those classes are in the java package and include all the classes you've seen so far in this book, plus a whole lot more classes you'll learn about later on in this book (and more you may not learn about at all).

The Java Developer's Kit comes with documentation for all of the Java class library, which includes descriptions of each class's instance variables, methods, constructors, interfaces, and so on. You can get to this documentation (called the Java Application Programmer's Interface, or API) via the Web at http://java.sun.com:80/products/JDK/CurrentRelease/api/packages.html. A shorter summary of the Java API is in appendix C as well. Exploring the Java class library and its methods and instance variables is a great way to figure out what Java can and cannot do, as well as how it can become a starting point for your own development.

Here are the class packages that are part of the Java class library:

  • java.lang-Classes that apply to the language itself, including the Object class, the String class, and the System class. It also contains the special classes for the primitive types (Integer, Character, Float, and so on). You'll get at least a glance at most of the classes in this package in this first week.
  • java.util-Utility classes, such as Date, as well as simple collection classes, such as Vector and Hashtable. You'll learn more about these classes in the Bonus Week.
  • java.io-Input and output classes for writing to and reading from streams (such as standard input and output) and for handling files. "Streams and I/O," describes the classes in this package.
  • java.net-Classes for networking support, including Socket and URL (a class to represent references to documents on the World Wide Web). You'll learn a little about "Windows, Networking, and Other Tidbits," "Client/Server Networking in Java."
  • java.awt-This is the Abstract Windowing Toolkit. It contains classes to implement graphical user interface features, including classes for Window, Menu, Button, Font, CheckBox, and so on. It also includes mechanisms for managing system events and for processing images (in the java.awt.Image package). You'll learn all about the awt in Week 2.
  • java.applet-Classes to implement Java applets.
In addition to the Java classes, your development environment may also include additional classes that provide other utilities or functionality. Although these classes may be useful, because they are not part of the standard Java library, they may not be available to other people trying to run your Java program unless you explicitly include those classes with your program. This is particularly important for applets, because applets are expected to be able to run on any platform, using any Java-enabled browser. Only classes inside the java package are guaranteed to be available on all browsers and Java environments.

0 comments:

Related Posts with Thumbnails