Re: Constructor



Lew wrote:
Philipp wrote:
If I write:
<code>
MyClass object = new MyClass(someArgument);
object.doStuff();
</code>

Do I have a guarantee that object is not null when I call doStuff?

Yes.

'new' cannot return 'null'.

Or is there a possible execution path which reaches .doStuf() [sic] with object
being null?

Bo Vance wrote:
No such guarantee.
try {
MyClass object =
new MyClass(someArgument);
object.doStuff();
} catch (someE e) {
some handling
} // guaranteed object.doStuff() won't be called if
// new MyClass(someArgument) throws.
See the JLS subsection:
15.9.4 Run-time Evaluation of Class Instance Creation Expressions
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9.4>


This doesn't answer the OP's question. That's how the execution might *not* reach 'doStuff()', but they asked how it might *reach* 'doStuff()' with 'object' not assigned (or assigned 'null').

For that read the JLS on "definite assignment". It gets an entire chapter.
<http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html>

There are ways to reach a reference without it being assigned, basically involving failure to deal with exceptions.

Foo foo; // I hate including 'Class' or 'object' in identifiers
try
{
foo = new Foo();
}
catch ( Exception exc )
{
// completely fail to handle exc
}
foo.doStuff(); // error


Thank you. Hopefully my error has elicited responses that will have
made the subject clear for the op, Phil.

--
BV
.



Relevant Pages

  • Re: Constructor
    ... MyClass object = new MyClass; ... Do I have a guarantee that object is not null when I call doStuff? ... For that read the JLS on "definite assignment". ... Foo foo; // I hate including 'Class' or 'object' in identifiers ...
    (comp.lang.java.programmer)
  • Re: Identifying language concepts from sample code
    ... var foo = { ... 'Variable declaration', 'assignment', and 'object literal/initialiser' ... above assigns a reference to that one object to the Identifier - foo -. ... property being created on the execution context's Variable/Activation ...
    (comp.lang.javascript)
  • Re: How to understand this form (something) (param);
    ... but the assignment to window.onload is what makes it ... is necessary for the closure to be formed. ... When foo is executed, bar has foo's activation/variable object on its ...
    (comp.lang.javascript)
  • Re: WaitForSingleObject() will not deadlock
    ... Even though the assignment of value 2 is guaranteed ... compiler's guarantee), without the specific guarantee from the thread ... the *compiler* does not guarantee this. ... Any data written to memory after the new thread is ...
    (microsoft.public.vc.mfc)
  • Re: The finer points of postfix conditionals.
    ... foo if foo = 1 ... I.e. the assignment to foo in the conditional is not in-scope when the ... This has to do with the way Ruby deals with local variables and ... Please note also that the code "expr1 if var = expr2" can ...
    (comp.lang.ruby)