• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about System.out.print behavior

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help with this question ..




The answer is Sub1 2 dont understand how?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To print anything from this statement, JVM needs to evaluate the whole expression, so it sees that ga.i is 1 (I hope there's no confusion here), then it sees the " " after that there's a method call, so that method is called to know the return value, that method first prints Sub, then returns 2, so the whole expression in the println statement becomes 1 + " " + 2 and thus the output is Sub1 2

HTH
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the value of ga.i becomes 1??
Since Arabik ISA Greek and it is overloading i.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing such as overloading of instance variables. Read about this here...
 
Dhruv Arya
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ankit got what you meant !
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Ankit, for the explanation of -why Sub is printed 1st and then comes the values 1 and 2.

My question is, how is ga.i evaluated to 1? Clearly, ga is an Arabik object and Arabik class overrides the value of instance varibale i to 2, so at runtime should'nt it get the value 2 for i? If not, is runtime polymorphism applicable only to methods and not instance variables?

I hope I am making my doubt clear.
Thankyou,
Surya
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Surya Priyadarshini wrote:My question is, how is ga.i evaluated to 1? Clearly, ga is an Arabik object and Arabik class overrides the value of instance varibale i to 2, so at runtime should'nt it get the value 2 for i? If not, is runtime polymorphism applicable only to methods and not instance variables?



From an earlier post from this exact topic....


Ankit Garg wrote:There's nothing such as overloading of instance variables. Read about this here...



There's no such thing as overriding instance variables either.

Henry
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please remember that overriding is applicable only for instance methods and not instance variables.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic