• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Confusion in value passing

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers,

Below is the code snippet.

When I ran this program, I got the output 1 1 1. But I was expecting 2 2 2 as the actual object that we pass to the printAll method is B.
When we invoke the method printAll, the parameter obj of the printAll method will have the bit pattern to get to the actual object, that is B.
So when we try to print obj.i, shouldn't it be class B's i value..?

Can someone please help me understand this..?
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

It is because instance variable are decided at compile time.

Since method take a reference (obj) of "A" type , it will print 1 everything we invoke printall method

foe eg -

Change method to



This will print 0 0 0

So , underlying concept to understand from this question is that Instance variables are decided at compile time.
Polymorphic behaviour is not there for instance variables
 
Soumya Manjunath
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

I modified the code a little bit.


The output is:


1
Class B
1
Class B
1
Class B



If the printAll method can print Class B, it is because of the polymorphic behaviour, right..? Then why not 2 as the value of obj.i ?

Please explain.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude because in the all the cases it is the reference A whose instance variable i is being referred. Because in the printAll method, the parameter is of type A but at runtime the reference A refers to the object B therefore "Hello B" is being printed......


Man i am on fire....
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know the variable always depends on reference type.

If you look at the reference type in printAll method it is of type A whose i value is 0

hence it prints 1 in all the cases.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ siva: that was simple but much clear
 
reply
    Bookmark Topic Watch Topic
  • New Topic