Chapter: Java The Complete Reference - The Java Library - Exploring java.lang

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Class - java.lang

Class encapsulates the run-time state of a class or interface. Objects of type Class are created automatically, when classes are loaded. You cannot explicitly declare a Class object.

Class

Class encapsulates the run-time state of a class or interface. Objects of type Class are created automatically, when classes are loaded. You cannot explicitly declare a Class object. Generally, you obtain a Class object by calling the getClass( ) method defined byObjectClass is a generic type that is declared as shown here:

 

class Class<T>

Here, T is the type of the class or interface represented. A sampling of methods defined by Class is shown in Table 17-15.

 









The methods defined by Class are often useful in situations where run-time type information about an object is required. As Table 17-15 shows, methods are provided that allow you to determine additional information about a particular class, such as its public constructors, fields, and methods. Among other things, this is important for the Java Beans functionality, which is discussed later in this book.

The following program demonstrates getClass( ) (inherited from Object) and getSuperclass( ) (from Class):

 

// Demonstrate Run-Time Type Information.

 

class X { int a; float b;

 

}

 

class Y extends X { double c;

 

}

 

class RTTI {

 

public static void main(String args[]) { X x = new X();

 

Y y = new Y(); Class<?> clObj;

 

clObj = x.getClass(); // get Class reference

System.out.println("x is object of type: " +

 

clObj.getName());

 

clObj = y.getClass(); // get Class reference

System.out.println("y is object of type: " +

 

clObj.getName()); clObj = clObj.getSuperclass();

System.out.println("y's superclass is " +

 

clObj.getName());

 

}

 

}

 

The output from this program is shown here:

 

x is object of type: X 

y is object of type: Y 

y’s superclass is X


ClassLoader

 

The abstract class ClassLoader defines how classes are loaded. Your application can create subclasses that extend ClassLoader, implementing its methods. Doing so allows you to load classes in some way other than the way they are normally loaded by the Java run-time system. However, this is not something that you will normally need to do.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.