• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is it so?  RSS feed

 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Statement 1 is printing 0 and
Statement 2 is printing 1.

Why is it so?
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hemanth,
When u call z().i, the statement j=1 is not executed by that time. so, i=peek() returns default value of j.
But in case of z().peek(), before calling any method of a class, all the fields(variables) are assinged to thier values. So j which is assinged to 1 is returned.

Hope this helps,
Please correct me if i am wrong.

Amrutha
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you're right. Try changing the order of the declarations.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



What I don't understand is when you run the above code, obviously the JVM complains that it cannot resolve symbol k.
If the JVM is smart enough to resolve and find whether the returned value is declared or not, why don't it just fetch the value of j too.
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is what I am unable to get too.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!