Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Class class

Like a human thinking about his/her own thinking, the process of a running program examining its classes is called reflection or introspection.

There is run-time information about classes that you can access. The central class representing this information is java.lang.Class, a class confusingly named Class.

Use the Object getClass() method to get a Class object

Every class has the class Object as an ancestor. A reflection method in the Object class is getClass(), which returns the Class of that object.

Class methods

java.lang.Class has two useful methods: getName() for getting a string name of the class and getSuperclass() for getting the Class object of the parent class, or null if this is the Object class. For example,

String s = "abc";
Class cls;
cls = s.getClass();  // Represents the String class.
System.out.println(cls.getName()); // Prints "java.lang.String"

Example - What class is the content pane?

I wanted to know the type returned by the JFrame getContentPane() method. The documenation says it returns a Container object, but that only means that it returns Container or a subclass of Container. This program prints the inheritance hierarchy for any object.

// Introspect.java -- Fred Swartz -- 2003.05.04
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;

class Introspect {

    //====================================================== method main
    public static void main(String[] args) {
        JFrame f = new JFrame();
        
        Container c = f.getContentPane();
        System.out.println("Content pane inheritance hierarchy");
        printAncestors(c);
        
        LayoutManager lm = c.getLayout();
        System.out.println("\n\nLayout for content pane inheritance hierarchy");
        printAncestors(lm);
        
        System.out.println("\n\nDefault JPanel layout inheritance hierarchy");
        printAncestors((new JPanel()).getLayout());
        
    }// end main
    

    //============================================ method printAncestors
    private static void printAncestors(Object obj) {
        ArrayList ancestors = getAncestorList(obj);
        for (int i=ancestors.size()-1; i>=0; i--) {
            Class cls = (Class)ancestors.get(i);
            System.out.println("   " + cls.getName());
        }
    }//end printAncestors
    

    //=========================================== method getAncestorList
    private static ArrayList getAncestorList(Object obj) {
        ArrayList result = new ArrayList();
        result.add(obj.getClass());
        
        for (Class parent=obj.getClass().getSuperclass(); 
                   parent != null; 
                   parent = parent.getSuperclass()) {
            result.add(parent);
        }
       
        return result;
    }//end getAncestorList
    
}//end class

The output from this program is

Content pane inheritance hierarchy
   java.lang.Object
   java.awt.Component
   java.awt.Container
   javax.swing.JComponent
   javax.swing.JPanel


Layout for content pane inheritance hierarchy
   java.lang.Object
   java.awt.BorderLayout
   javax.swing.JRootPane$1

   
Default JPanel layout inheritance hierarchy
   java.lang.Object
   java.awt.FlowLayout

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.