Header Ads

  • Breaking Now

    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.


    1. 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.

    2. 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:


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

    3. 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


    Post Top Ad

    Post Bottom Ad