wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on statics (from SCJP 6 by Kathy Sierra and Bert Bates) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on statics (from SCJP 6 by Kathy Sierra and Bert Bates)" Watch "Question on statics (from SCJP 6 by Kathy Sierra and Bert Bates)" New topic
Author

Question on statics (from SCJP 6 by Kathy Sierra and Bert Bates)

Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 43

Questions about the following: (from SCJP 6 Study Guide by Kathy Sierra and Bert Bates. Chapter 2: Statics)



Output: a a a

Why? On second thought, I guess I knew the answer, it's because the polymorphic method invocations where the method of the instance of the object (not the reference variable) is invoked only applies to instance methods (not static). If I'm right, my question would be, how what is that redefinition does?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

static methods are not overridden. in your case static method of superclass is hidden by static method of the same name. you are right - there is no dynamic method dispatch that happens during overrding whereby the method is invoked on the instance of the object at the other end of reference. static methods are class methods - they are not tied to any instance/object. you can invoke class methods on the reference variables although it is not recommended. taking your example when you do

Animal a = new Animal();

a.doStuff(); // it actually transforms into Animal.doStuff()\

even if you have Animal a = null;

a.doStuff(); // will work and wont give NPE
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on statics (from SCJP 6 by Kathy Sierra and Bert Bates)