Forums Register Login

What does the period do?

+Pie Number of slices to send: Send
Beginner here,

This is from another thread:  https://coderanch.com/t/245950/certification/Exam-Prep-post-increment I read but I could not understand what the JMM125.i on the last line of code means. Especially that period between the JMM125 and i. What does it do to make it come out as a 3?

Explanation? Thank you!



Product:

1
2
1
2
0
1
3
+Pie Number of slices to send: Send
I think the idea is to show that the three local variables called i are distinct from the static field called i. Note the scopes of the three local variables must not overlap.
+Pie Number of slices to send: Send
On line 3 you have an instance class variable called i.  That variable can be seen in all methods except when it is hidden by a local variable.  On line 12 you have a local variable also called i that hides the instance class variable.  Line 16 accesses the instance class variable directly.  It is 3 because you use it in line 6.

[Edit: missed that the first i is static.]
+Pie Number of slices to send: Send
My mistake: there are only two local variables.
+Pie Number of slices to send: Send
 

Peter Park wrote:Beginner here,

This is from another thread:  https://coderanch.com/t/245950/certification/Exam-Prep-post-increment I read but I could not understand what the JMM125.i on the last line of code means. Especially that period between the JMM125 and i. What does it do to make it come out as a 3?

Explanation? Thank you!



Product:

1
2
1
2
0
1
3



Hi, Peter,

When you loop within the static main method, you don't declare a new int variable in your loop, you assign a value to one that already exists, the static one. You don't have to qualify the static variable's name with class name if you don't want to. It's a tricky piece of code. I would always qualify for readability.

So basically, it increments the static i variable and then prints out its last value.

With best regards,

Anton.
+Pie Number of slices to send: Send
Thanks for the replies! I think I understand it.
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 418 times.
Similar Threads
Variable scope confusion
Doubt in For Loop..
shadowing local variable
Doubt in For loop
Shadowing Doubt again?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:52:32.