Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Method overriding and variables hidding  RSS feed

Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

So I have the following code:

First of all I'm not sure if my question is actually about method overriding because I'm not sure what causes this. I understand why the result is(I mean how the overriding and hiding works) :

Beta 44
4 44
Beta 44
44 44

What I don't understand is why is not:
4 Beta 44
44 Beta 44

Operations in a System.out.println(), are done from left to right in this case, no? So "4" should have been first because it was hidden; then the method from Beta class comes next, because is was overridden: Beta 44; then the println from inside the method; and last the return from the method: 44, right? For the next System.out.println() is the same process only that h is 44....

Why is "System.out.println("Beta " + h)" from the b.getH() read first, then "b.h", then the return "44" from  b.getH() and last the println from the original System.out.println()?
Posts: 12318
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling the methods from inside the () for the println method calls on lines 16 and 18. That means the called methods will be evaluated first. Since those methods also call System.out.println() on lines 4 and 11, those effectively become "nested" calls. The inner nested calls to println() are executed before the outer ones.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!