The Class Named Class

     

There is a class called Class that represents the possibility of making and loading classes in the Java Programming Language. You can make instances of the Class class for the classes and interfaces in a running Java application.

The primitive Java types and the keyword void are represented as Class types.

Class has no public constructor. That is, you cannot write this: Class clazz = new Class(); . You can get a Class object for a given class in three different ways.

First, you can use the getClass() method of java.lang.Object. If you have an instance of Object called obj , you can write the following:

 

 Class clazz = obj.getClass(); 

Second, you can use the class literal, like this:

 

 Class clazz = java.lang.String.class; 

Third, you can get it by passing a String containing the fully qualified name of the class of which you want a class instance, like this:

 

 try {     clazz = Class.forName("java.lang.String"); } catch (ClassNotFoundException e) { } 

Getting the Package of a Class

You can also get the package that a class is in by calling the getPackage() method on the Class object:

 

 Class clazz = java.lang.String.class; Package package = clazz.getPackage();           //represents a package String pName = package.getName();           // java.lang 

If you write a class without declaring a package for it, it will be created in the default package, which has no name. If you call getPackage() on the Class object of such a class, it returns null. It also returns null for a primitive type class, or an array. Like this:

 

 Class clazz = SomeClass.class; packg = clazz.getPackage();          // null packg = char.class.getPackage();     // null packg = char[].class.getPackage();   // null 

Getting an Object's Superclass

You can get an object's superclass by calling the getSuperclass() method on the Class object.

 

 Object obj = new String(); Class supr = obj.getClass().getSuperclass();               // java.lang.Object // The superclass of java.lang.Object is null obj = new Object(); supr = obj.getClass().getSuperclass();     // null 

Listing the Interfaces That a Class Implements

We will find out about interfaces later. For now, know that they are a contract containing methods that you can force a class to implement. Many classes in the Java API implement interfaces. You can list all of the interfaces implemented by a class with the following code:

 

 Class cls = java.lang.String.class; Class[] interfaces = cls.getInterfaces(); 

This stores the Class objects of all of the interfaces String implements in an array.

Note that if the reference type of your item is an interface, the call to

 

 intfc.getClass.getSuperclass(); 

returns the object's superclass.

< shameless -plug>This is enough for now about the very cool java.lang.Reflect package. If you are interested in finding out about classloading , dynamic proxy creation, how to dynamically invoke a method, and even how to change the programmed visibility of a member using the reflection API, you'll have to get the sequel to this book, the inventively titled More Java Garage . </shameless-plug>

Now that we are on more solid ground with the fundamental structures in Java, it will get a lot easier now to start working with more code, seeing more useful examples, and starting to do some fairly exciting things.

The Java world gets markedly more complex at this point, and that sophistication will pay off for you in power.

We won't waste any time. The next chapter introduces one of the three cardinal precepts of Object-Oriented programming: for the sake of clarity, they are Faith, Hope, and Inheritance .

Okay, they are Encapsulation, Polymorphism, and Inheritance.

That somehow sounds less dramatic, though. Onwards and upwards.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net