Variable scope and redefinitions

B

biffta

I came across the following code in a textbook and am a little puzzled
by it:

ArrayList<JCheckBox> checkboxList = new ArrayList<JCheckBox>();

for (int i = 0; i < 256; i++) {

JCheckBox c = new JCheckBox();

c.setSelected(false);

checkboxList.add(c);

mainPanel.add(c);

} // end loop

My question is this; How can you re-declare the variable c over and
over? I mean if you were to write:

JCheckBox c = new JCheckBox();
JCheckBox c = new JCheckBox();

The compiler would report an error. I presume it has something to do
with the scope within the for loop? That kind of makes sense I suppose
but what about when you leave the loop, what is the name of the
variable at first position of checkboxList? Or the second or the third?
Or is it simply that it doesn't matter because it is only a reference
to a JCheckBox?

Thanks for reading!
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

biffta said:
I came across the following code in a textbook and am a little puzzled
by it:
for (int i = 0; i < 256; i++) {
JCheckBox c = new JCheckBox();
} // end loop

My question is this; How can you re-declare the variable c over and
over? I mean if you were to write:

JCheckBox c = new JCheckBox();
JCheckBox c = new JCheckBox();

The compiler would report an error. I presume it has something to do
with the scope within the for loop?

Yes. c goes out of scope for each iteration.
That kind of makes sense I suppose
but what about when you leave the loop, what is the name of the
variable at first position of checkboxList? Or the second or the third?
Or is it simply that it doesn't matter because it is only a reference
to a JCheckBox?

Correct again.

Arne
 
F

Fred Kleinschmidt

biffta said:
I came across the following code in a textbook and am a little puzzled
by it:

ArrayList<JCheckBox> checkboxList = new ArrayList<JCheckBox>();

for (int i = 0; i < 256; i++) {

JCheckBox c = new JCheckBox();

c.setSelected(false);

checkboxList.add(c);

mainPanel.add(c);

} // end loop

Consider a slightly different way of writing the same thing:
for (int i = 0; i < 256; i++) {
JCheckBox c;
c = new JCheckBox();
c.setSelected(false);
checkboxList.add(c);
mainPanel.add(c);
}

Note that "c" is not being re-declared, but just re-assigned.
 
B

biffta

Consider a slightly different way of writing the same thing:
for (int i = 0; i < 256; i++) {
JCheckBox c;
c = new JCheckBox();
c.setSelected(false);
checkboxList.add(c);
mainPanel.add(c);
}

Note that "c" is not being re-declared, but just re-assigned.

I created an image which I hope correctly illustrates this code.

http://www.hopkins81.com/wp-images/scope-variables.PNG

I guess my confusion arose from having an arrayList full of C's but I
think now that this just doesn't matter!
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top