Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Dec 1999 00:58:19 +0100 (MET)
From:      Rolf Grossmann <grossman@securitas.net>
To:        freebsd-java@FreeBSD.org
Cc:        java-port@FreeBSD.org
Subject:   Duplicate Class initialization?
Message-ID:  <199912152358.AAA23703@blue.securitas.net>

next in thread | raw e-mail | index | archive | help

--7jGBHTmUit
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit

Hi all,

I'm having a small problem (what seems to be a bug) with the FreeBSD JDK.
The static initializer for a class is called the first time when that
class is referenced from a third class that is loaded dynamically and
a second time after a socket connection has been accepted and the class
is referenced from the main program. The exact scenario can be seen from
the attached test case. It is the simplest test case I was able to figure
out. It does not happen when you access the Class directly before (or
without) accepting the socket.

To verify the problem, compile the 3 attached classes and run java Main.
You will see the classes being initialized. From another shell, make a
socket connection to port 2222 on that host. Wonder why Class1 is
being initialized again.

The problem does not occur on Solaris, so I guess it's FreeBSD specific.
It also does not happen when using TYA. I'm using the
jdk1.1.8_ELF.V1999-11-9 on FreeBSD 3.3-STABLE (as of 2-3 months ago).

Bye, Rolf

--7jGBHTmUit
Content-Type: text/plain
Content-Description: Main.java
Content-Disposition: inline;
	filename="Main.java"
Content-Transfer-Encoding: 7bit

import java.lang.Class;
import java.net.*;
import java.io.*;

class Main {
public static void main (String args[]) {
    ServerSocket sock;

    try {
	sock = new ServerSocket(2222);
    } catch(IOException e) {
	System.exit(1);		// Security may prevent exit ...
	return;			// but not return
    }

    try {
	Class.forName("Class2");
    } catch(ClassNotFoundException e) {
    }

    try {
	Socket s = sock.accept();
	Class1.fn();
    } catch(IOException e) {}

}

}

--7jGBHTmUit
Content-Type: text/plain
Content-Description: Class1.java
Content-Disposition: inline;
	filename="Class1.java"
Content-Transfer-Encoding: 7bit

import java.util.Vector;

class Class1 {
private static Vector v = new Vector();

static {
    System.out.println("Init Class1");
}
    
public static void register(Class2 c) {
    v.addElement(c);
    System.out.println("register");
}

public static void fn() {}

}

--7jGBHTmUit
Content-Type: text/plain
Content-Description: Class2.java
Content-Disposition: inline;
	filename="Class2.java"
Content-Transfer-Encoding: 7bit

class Class2 {

static {
    System.out.println("Start Class2");
    Class1.register(new Class2());
    System.out.println("End Class2");
}
    
private Class2() {
}

/* ... */

}

--7jGBHTmUit--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-java" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199912152358.AAA23703>