• 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

Inherited Static Variables

 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me understand what is going on. This is from K&B's Practice Exams book, practice exam 2, question 47.

I modified the code by adding some comments and the extra System.out.println on lines 42 and 43. What I don't understand is: which "s" am I modifying all along? Always the one belonging to the superclass? Always the one belonging to the subclass? How come they are both the same in the end??

 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output is:

gt Tool gt myRake
gt Tool gt myRake

Process completed.




so both static variables have been modified?
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chandella wrote:so both static variables have been modified?


Buenos días Chandella...
Point of correction there is NO "both" static variables, rather there is a "single" static variable shared by all intances of a class and (to the best of my knowledge) all instances of the subclasses. REMEMBER that Rake "Is-a" GardenTool.

HTH

Regards

Ikpefua
 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Buenos dias

Hmm... that's interesting, I didn't know that. So I can always assume that a class will inherit all its ancestors' static variables, and I will be able to call them by using the name of my class and the name of the variable? I'll do some testing to find out. Thank you
 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah... you are absolutely right, Ikpefua!

 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi there.
We are all the same

Process completed.

 
reply
    Bookmark Topic Watch Topic
  • New Topic