• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does this static variable change in a for loop?  RSS feed

 
Nick David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. I'm getting a solid understanding of my basics in Java this summer. I had a tutor recommend a site called "practice it", and it's pretty nifty. Anyways, one problem they have has this bit of code:

My question is, why does the MAX change inside the for loop? I thought that once a data type was declared "final" that it was unalterable. Any help would be awesome.
Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick David wrote:
My question is, why does the MAX change inside the for loop? I thought that once a data type was declared "final" that it was unalterable. Any help would be awesome.


The MAX variable did *not* change. You set the value to the count variable, which changes during different iterations of the loop.

Henry
 
Nick David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm kicking myself with how simple that solution is. Thanks for the explanation.
 
SunilK Chauhan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nick,

Java supports only pass by value not pass by reference. For more reference you can go through the document: http://javadude.com/articles/passbyvalue.htm
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick David wrote:I'm kicking myself with how simple that solution is. Thanks for the explanation.

We've all done the same in one situation or another.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SunilK Chauhan wrote:...Java supports only pass by value not pass by reference...

I didn't quite understand how this relates to the current question, MAX is not even a reference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!