B
bob smith
I'm trying to come up with a mnemonic that helps me remember which variables get auto-initialized in Java. Is it just locals that you have to explicitly initialize?
I'm trying to come up with a mnemonic that helps me remember which
variables get auto-initialized in Java. Is it just locals that you
have to explicitly initialize?
I'm trying to come up with a mnemonic that helps me remember which variables get auto-initialized in Java. Is it just locals that you have to explicitly initialize?
Fields (both instance fields and static fields) are always
initialized to zero/false/null as appropriate.
Parameters of methods and constructors are always initialized
by the caller's argument expressions.
The variable in a `catch' clause is initialized to refer to
whatever was thrown.
Nothing else is initialized until and unless you initialize it.
I'm trying to come up with a mnemonic that helps me
remember which variables get auto-initialized in Java.
Is it just locals that you have to explicitly initialize?
Roedy said:bob smith wrote, quoted or indirectly quoted someone who said :
you have static, instance and locals.
Anything else?
Nope.
Of those, only locals need explicit initialisation, but you can do
things like this:
int n;
if ( a > 1 ){ n = 1; }
else { n = 2; }
you could of course abbreviate that to
int n = (a > 1) ? 1 : 2 ;
Lew said:The semantics of initialization, the second form Roedy shows, are slightly different
from the semantics of program execution in the first form.
For example, the second form allows the variable to be 'final'.
For example, the second form allows the variable to be 'final'.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.