Amazing Java

Home| JDBC| Sudoku| Applet| Produkte

The power of reflection

/******************************************************************************/
/*                                                                            */
/*                                                      FILE: JavaGrusel.java */
/*                                                                            */
/*  This proves that Java is much easier to understand than C++               */
/*  ===========================================================               */
/*                                                                            */
/*  You need at least JDK1.3 to compile and run this code                     */
/*                                                                            */
/*  V1.00   22-AUG-2002   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

import java.lang.reflect.* ;

interface Grusel {
  String Hello() ;
  String world() ;
}

public class JavaGrusel {
  public static void main( String args[] ) {
    Grusel g = (Grusel) Proxy.newProxyInstance(
                          Grusel.class.getClassLoader(),
                          new Class[] { Grusel.class },
                          new InvocationHandler() {
                            public Object invoke( Object proxy,
                                                  Method m,
                                                  Object[] args )
                                                  throws Throwable {
                              return m.getName() ;
                            }
                          } ) ;
    System.out.println( g.Hello() + " " + g.world() + " !" ) ;
  }
}

An inner inner inner class

/******************************************************************************/
/*									      */
/*							   File: Amazing.java */
/*									      */
/*	Demonstrates an inner innner inner class			      */
/*	========================================			      */
/*									      */
/*	Compiled and tested with JDK1.2					      */
/*									      */
/*	V1.00	24-NOV-1999	P. Tellenbach	https://www.heimetli.ch/      */
/*									      */
/******************************************************************************/

class Amazing {
  public static void main( String args[] ) {
    for( int i = 0; i == 0; i++ ) {
       class Really {
          class Quite {
             class Surprising {
                public String toString() {
                   return( "it really works !" ) ;
                }
             }
          }
       }
       System.out.println( "It may be hard to believe, but " + ((new Really()).new Quite()).new Surprising() ) ;
    }
  }
}

Overloading a method of an anonymous Object

/******************************************************************************/
/*									      */
/*							      File: Joke.java */
/*									      */
/*	Demonstrates an anonymous inner class				      */
/*	=====================================				      */
/*									      */
/*	Compiled and tested with JDK1.2					      */
/*									      */
/*	V1.00	24-NOV-1999	P. Tellenbach	https://www.heimetli.ch/      */
/*									      */
/******************************************************************************/

class Joke {
  public static void main( String args[] ) {
    System.out.println( new Joke() { public String toString() { return "You must be joking !" ; } } ) ;
  }
}

Another great program

/******************************************************************************/
/*									      */
/*							   File: Elegant.java */
/*									      */
/*	Demonstrates the elegance of java				      */
/*	=================================				      */
/*									      */
/*	Compiled and tested with JDK1.2					      */
/*									      */
/*	V1.00	24-NOV-1999	P. Tellenbach	https://www.heimetli.ch/      */
/*									      */
/******************************************************************************/

class Elegant {
  public static void main( String args[] ) {
    (new Thread( new Runnable() {
        public void run() {
           System.out.println( "Isn't it great ?" ) ;
        }
    } ) ).start() ;
  }
}