[670] in java-interest

home help back first fref pref prev next nref lref last post

lifetime of objects

daemon@ATHENA.MIT.EDU (Lein Ton)
Wed Jul 12 17:44:59 1995

Date: Wed, 12 Jul 1995 16:27:03 -0400
From: ton@cc.gatech.edu (Lein Ton)
To: java-interest@java.sun.com

Hi all,

I can remember that creating new objects in C++ within methods was one
of the most certain way of getting a core dump.
The next java program seems to work, but can I be sure that the
instances that created in add() of HtContainer have the same lifetime
as the hashtable in HtContainer?

Have fun,
Lein Ton

------------------------------------------------------------------------------

import java.util.*;

class test4 extends Hashtable {
	static HtContainer aHtContainer; // container of a hashtable
	public static void main(String args[]) {
		aHtContainer = new HtContainer();
		aHtContainer.output();
	}
}

class HtContainer {			// container of a hashtable
	Hashtable ht;
	public HtContainer() {
					// create a new hastable, and fill it
		ht = new Hashtable();
		for (int i = 1; i<100; i++) {
			add();
		}
	}
	public void output() {
					// show all the hashtable elements
		for (Enumeration e = ht.elements(); e.hasMoreElements();)
		{
			((StringContainer)e.nextElement()).output();
 		}
	}
	public void add() {
					// add a newly created element to ht
		ht.put(Integer.toString(ht.size()), 
			new StringContainer(Integer.toString(ht.size())));
	}
}
class StringContainer {			// container for a String
	String str;
	public StringContainer(String aString) {
		str=aString;
	}
	public void output() {
		System.out.println(str);
	}
}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post