Walter Bright escribió:
Ary Borenszweig wrote:
Walter Bright escribió:
Ary Borenszweig wrote:
It's not like that. They don't require you to initialize a variable
in it's initializer, but just before you read it for the fist time.
That's very different.
The only way to do that 100% reliably is to instrument the running code.
Java does it on compile time.
Java is a severely constrained language. Even so, how does it do with this:
Foo f;
if (x < 1) f = new Foo(1);
else if (x >= 1) f = new Foo(2);
f.member();
Whenever there are branches in code and a variable still doesn't have a
value at that point:
- if all branches assign a value to that variable, from now on the
variable has a value
- if not, at then end of the branches the variable still doesn't have
a value
? (You might ask who would write such, but sometimes the conditions are
much more complex, and/or are generated by generic code.)
If it's done only for local variables then you don't need to
instrument the running code.
How about this:
Foo f;
bar(&f);
? Or in another form:
bar(ref Foo f);
Foo f;
bar(f);
Java doesn't have ref parameters.
C# does have ref parameters and it also performs this kind of check. I
just tried it and it says a parameter can't be passed by reference if it
doesn't have a value assigned. So your first example should be an error.
The same should be applied for &references.
(in your first example, if you want to pass f by reference so that bar
creates an instance of f, then it should be an out parameter).