Sun's java vs. gij : Own classloader and static attributes

Stefan Prelle prelle@informatik.uni-bremen.de
Tue Jun 22 16:05:00 GMT 2004


Hi folks,

I just stumbled over a different behaviour of how the java interpreters
handle static variables in classes loaded using a self written
classloader.

I am using a class containing a static attribute (e.g.)
  public class MyStatic {
    static int value;  
  }

I defined my class loader that has a loadClass-Method which simply loads
the class data into an array:

    public Class loadClass(String name) throws ClassNotFoundException {
	System.out.println("MyClassLoader.loadClass("+name+")");   
	byte[] data = ... obtain class data
	Class c = defineClass(name, data, 0, len);
	resolveClass(c);
	return c;
    }

Using this class loader I load a simple class:

public class Loaded {
    public Loaded() {
	System.out.println("In Loaded: Value is "+MyStatic.value);
    }
}

The following test program 

public class Test {
    public static void main(String[] args) throws Exception {
	MyStatic.value = 5;
	System.out.println("Value is "+MyStatic.value);
	MyClassLoader loader = new MyClassLoader();
	Class cls = loader.loadClass("Loaded");
	cls.newInstance();	
    }
}

produces different results when executed using "java" or "gij".

---Using Sun's java----------------------
Value is 5
MyClassLoader.loadClass(Loaded)
MyClassLoader.loadClass(java.lang.Object)
MyClassLoader.loadClass(java.lang.System)
MyClassLoader.loadClass(java.lang.StringBuffer)
MyClassLoader.loadClass(MyStatic)
MyClassLoader.loadClass(java.io.PrintStream)
In Loaded: Value is 0
MyClassLoader.loadClass(java.lang.Class)

---Using GNU gij--------------------------
Value is 5
MyClassLoader.loadClass(Loaded)
In Loaded: Value is 5


I figured that I still don't use my ClassLoader subclass the correct
way, since it should only load that classes that haven't been loaded so
far and I think I better overwrite the findClass-Method. 
But anyway: Is the different behaviour of gij considered a bug or
feature?

Regards,
  Stefan

-- 
-----------------------------------------------------
Stefan Prelle
eMail: stefan@prelle.org
ICQ  : 9800462

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
URL: <http://gcc.gnu.org/pipermail/java/attachments/20040622/74b14e2b/attachment.sig>


More information about the Java mailing list