locals es loco?

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?
 
M

markspace

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 think so. There's class variables, instance variables, local
variables, and method arguments off the top of my head. The first two
are always initialized to null/0/false if you don't init them.
Technically method arguments need to be initialized, but the compiler
won't let you call a method without supplying all of the arguments.
 
E

Eric Sosman

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.
 
L

Lew

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.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-16.html
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2
http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3.1
 
R

Roedy Green

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?

you have static, instance and locals.
Anything else?

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 ;
 
L

Lew

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 ;

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'.

Also notice Roedy's use of parentheses in the ternary expression. The language doesn't
require them, but he uses them to improve readability of the expression.

I don't know of any language where the ternary operator's precedence is higher than
the comparison operator's, nor where assignment trumps the ternary. Still, the
plethora of obscure operators tends to obscure meaning, so many programmers use
parentheses this way. It's considered good practice.

Not that that resolves all controversy. Some might prefer

int n = (a > 1 ? 1 : 2);

I for one would not denigrate

int n = a > 1 ? 1 : 2;

as a Java code reviewer, but I'd accept the comment as a reviewee.
 
A

Andreas Leitgeb

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'.

So does the first.
 
R

Roedy Green

For example, the second form allows the variable to be 'final'.

So can the first. I do it all the time. I like to insert final every
possible place it can go.

The compiler is quite clever about detecting missing initialisation
code. For example if you declare prior to a switch for example, the
compiler will complain if you forget to initialise it in one of the
cases.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,306
Members
47,953
Latest member
TrinidadV1

Latest Threads

Top