• Post Reply Bookmark Topic Watch Topic
  • New Topic

understanding the code  RSS feed

 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys! I have two classes:


and main class:



when i run the code, i get in the end:

Checking the temperature.
Status: returning home
Speed: 5
Temperature: 670.0

my question is, why as a value for variables, it took the value from the class, not from the main class

Thanks!

[edited to fix code tags]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
showAttributes() is an instance method in VolcanoRobot. This means it sees the instance variables of VolcanoRobot, not main, when it runs.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Horst wrote:
my question is, why as a value for variables, it took the value from the class, not from the main class

Frankly speaking, I didn't understand your question. Can you elaborate, what's your issue with the output? What did you expected it to print? It is printing as expected. You're just showing us the output of the last "dante.showAttributes();` call. Before that, you have the below invocation:
And in "checkTemperature()", you are changing the value of "status" and "speed", if the temperature value is greater than "660", which is true, since you actually set the value of "temperature" to "670", in the following statement:
So, the value of "status" and "speed" is as expected.
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i posted only the part of the result i had a doubt about.the whole result is :
Status: exploring // dante.showAttributes(); - here code reads values from main method
Speed: 2
Temperature: 510.0
Increasing speed to 3.
Status: exploring // dante.showAttributes(); same a sabove, but with another speed value
Speed: 3
Temperature: 510.0
Changing temperature to 670.
Status: exploring // same, only temp is changed
Speed: 3
Temperature: 670.0
Checking the temperature.
Status: returning home // here is my question: why code reads values for "status" and "speed" from class, not main class
Speed: 5
Temperature: 670.0
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!