Wednesday, April 18, 2007

What are the restrictions for static method?

Whenever you say something is static that means data or method is not associated with an object instance of that class.They are allocated when a class is loaded,during compile time. Only a single copy of that will be created for that class. So even if you have never created an object of a class you an always access static data and method of that class. If you have class by name 'Vehicle' and you have a static method 'drive()' then it can simply be invoked by ' Vehicle.drive()', no need of object cretaion in this scenario.A static method cannot access non static data and can invoke other static methods.All static methods are automatically final. It is redundant to make them final.

3 comments :

Anonymous said...

I don't see any reason why i can't override a static method from a child static context. I have tried this in jdk6 without compilation problems. After declaring the child method final I get a compilation error.

Therefore a static method is not by default final.

Deepun on February 13, 2009 at 5:24 AM said...

Yes..even I agree to this fact, one can override a static method but not final static method. Just refer the following bug at Sun website:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=1224787

This is real controversial to say static methods are implicitly final. :))

Alex Kuz said...

Saying that static methods are implicitly final is completely wrong simple because each class can have it's own main() method, unless is declared final in parent

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker