Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

code explanation  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am reading this document

http://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html

And I made following code



and i got following output

a.y = 5
b.y = 6
a.x = 2
b.x = 2
IdentifyMyParts.x = 2



in thrid line output is a.x=2;

As per what i read it should be 1 to there


any explanation?


Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at questions 1a and 1b in the link. Do you know what is meant by class and instance variables ? If not you need to go back and review the tutorial.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the class variable and instance variable but how output a.x=2; is linked with them?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you've been reading, but the output is correct - a.x and b.x are actually the same variable. Can you explain why they are the same, and why -in contrast to that- a.y and b.y are different variables?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it thanks to both
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!