• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Looping Constructs  RSS feed

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: This program will compile and run but will never terminate. There is a local variable x in the method looper() that shadows the instance variable x. Nevertheless I do not understand how it works in details; the method getX() never returns 0. In the method looper() the local variable x gets 0, but the method getX() does never returns 0. Does the method get() always refer to the instance variable which has the value 5?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:Does the method get() always refer to the instance variable which has the value 5?


Well... yes. That is the only variable 'x', that is in the scope of the getX() method.

Henry
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The local variable x is assigned 0 on line 12. It will also be 0 when the for-loop on line 14 terminates. However, on line 13, it is assigned the value return by getX(), which is always 5. So, because of the assignment in the while loop condition, your local variable x will always be 5 when the while loop condition is evaluated, thereby making it a non-terminating condition.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!