wiki:Examples/ObjectModel

/* ***************************************************************************
 * JavaParty
 *
 * Copyright (C) 1996-2002 The JavaParty Team, University of Karlsruhe
 *
 * Permission is hereby granted to modify and use this software for research
 * and teaching purposes. Modification for commercial purposes requires
 * prior written permission by the author.
 *
 * The software, or modifications thereof, may be redistributed only
 * if this copyright notice stays attached.
 *****************************************************************************/
/*
 * $Revision: 1.3 $
 * $Date: 2002/04/05 12:49:40 $
 */

package examples;

/** Instances of this class are used to test parameter passing */
class Param implements java.io.Serializable {}

/**
 *  @author Bernhard Haumacher <haui@haumacher.de>

 */
public remote class ObjectModel {
    Param p;

    public void foo(Param p) {
        this.p = p;
    }

    static Param q;

    public static void bar(Param q) {
        ObjectModel.q = q;
    }

    public void test() {
        Param a = new Param();

        // invoke method of same object without a reference
        foo(a);
        if (this.p != a) throw new RuntimeException("(a)");


        // invoke method of same object with this reference
        this.foo(a);
        if (this.p != a) throw new RuntimeException("(b)");

        ObjectModel model = this;

        // invoke method of same object with arbitrary reference
        model.foo(a);
        if (this.p == a) throw new RuntimeException("(c)");

        // invoke static method from instance method
        bar(a);
        if (ObjectModel.q == a) throw new RuntimeException("(d)");

        // invoke static method from instance method with class name
        // reference
        ObjectModel.bar(a);
        if (ObjectModel.q == a) throw new RuntimeException("(e)");
    }

    public static void testStatic() {
        Param a = new Param();

        // invoke static method of same class 
        bar(a);
        if (ObjectModel.q != a) throw new RuntimeException("(f)");

        // invoke static method of same class with class name
        // reference

        // TBD: This does not work correctly and throws an exception
        // (see BUG/0061).
        //      ObjectModel.bar(a);
        //      if (ObjectModel.q != a) throw new RuntimeException("(g)");
    }

    public static void  main(String args[]) {
        new ObjectModel().test();
        testStatic();
        System.out.println("Test completed successfully.");
    }

}
Last modified 11 years ago Last modified on Mar 23, 2006 12:44:13 AM