[721] in java-interest

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

How does one tell if a variable is initialized?

daemon@ATHENA.MIT.EDU (Steve Drach)
Mon Jul 17 16:20:08 1995

Date: Mon, 17 Jul 1995 11:52:34 -0800 (PDT)
From: Steve Drach <Steve.Drach@Eng.Sun.COM>
Reply-To: Steve Drach <Steve.Drach@Eng.Sun.COM>
To: java-interest@java.Eng.Sun.COM

What's the proper or preferred idiom for determining whether a variable
contains a valid pointer to an object or not.  The try statement is a little
awkward if you want to do something with an initialized variable.  For
example:

Object o;
...

if (/* o is not null */) {
	// do stuff not using o, but necessary because o exists
	// do stuff using o
}
o = new Object();

Using a try statement, I have to do something like:

try {
	// do stuff not using o
	// do stuff using o
} catch (NullPointerException e) {
	// undo stuff done not using o -- Oops! may not be possible
}
o = new Object();

I'd really like to just say:

if (o != null)

-
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