• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime and Compiletime confilict  RSS feed

 
hemantha kumar k
Greenhorn
Posts: 27
Hibernate Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why this program returning the value as 3, even though "foo" Object refering the Bar Object on the Heap
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tinku rider wrote:
Why this program returning the value as 3, even though "foo" Object refering the Bar Object on the Heap


because variable dont have polymorphic behaviors
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tinku,

Accessing instance variables directly is also against the principles of java. Instead, you can use getter methods to return value of a and try running the program and verify the output.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
tinku rider wrote:
Why this program returning the value as 3, even though "foo" Object refering the Bar Object on the Heap


because variable dont have polymorphic behaviors

Correct; for static methods and fields it's the reference type (Foo in this case) that determines which version is returned. Polymorphism only holds for methods.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a rather similar discussion here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!