This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
When we declare any method as abstract then all the classes which extend that class will have to override that method. and if you declare a method with static modifier it means that it will be allocated one shared memory for all the objects of that class. So if you combine abstract and static then the memory will be shared accross all classes. Every class extending that top class with have to override the method and have different definition of that method for each class. So which definition will that method hold? As it is allocated in one shared memory block. Thats why we can't declare abstract method as static.
My understanding is that a static inner class is treated exactly as a top-level class (the class that the .class file is named after), and cna include any modifiers a top-level class can, including abstract. I don't believe you'd be able to directly instantiate the class, but you could write an other inner (or anonymous) class that implements the required methods. I believe that static in this case does not mean the same thing as static in a method or variable does.
Joined: Feb 06, 2004
I still get confused!! If I declare static inner class as abstract, why compile OK ?? Would anyone explain me clear??
Joined: Jan 22, 2004
An abstract inner class is not the same as a top-level class in one regard: the static keyword does not mean the same thing. In my understanding (someone please correct me if I'm wrong) the static keyword in an inner class is used to specify the inner class as a top-level nested class, not as class-specific data, such as a static int, for example. I hope this helps.
method declaration is not same as class declaration. if you declare abstract method as static, then it has no meaning . for example, when you access the static abstract method with Class reference, then what statement would be executed? so abstract and static combination prohibited in method declaration.
but in nested class declaration both can be used. the reason is when you say static in declaration, then the inner class is no more bounded with outer class's instance . and when you combine an abstract keyword to it, then it means other outer class can be extends the nested class.
does that make sense?
Joined: May 12, 2011
I am actually wondering what purpose this is used for in real time. For eg, the following code compiles,runs and uses Inheritance. This is as if the outer class is a Package and the static nested classes are like the classes. Is this kind of class structure used for any specific purpose? (static abstract declarations and static methods being overridden)
Is this kind of class structure used for any specific purpose?
The purpose of a static inner class is packaging convenience.
The purpose of an abstract static inner class is that it allows you to defer implementation of an abstract method to a suitable subclass.
Joined: May 12, 2011
Thanks What does packaging convenience imply in this case?
Joined: May 13, 2002
Packages allow you to group similar classes together. For example classes involved in mathematical calculations are grouped together in the java.math package. The BigDecimal class can be accessed like this java.math.BigDecimal.
Similarly, it makes sense to group together the Outer class and static Inner class if the Inner class is useful only to the Outer class. The Inner class can be accessed like this package_name.Outer.Inner. Non-static inner classes have a similar packaging structure, however, they are more tightly bound to the Outer class.
When a method is abstract, it means that you're not supplying the implementation of the method at that moment; it must be overridden in a subclass, where the implementation will be added.
You can see now that the combination static and abstract is impossible for methods: an abstract method must be overridden, but static methods cannot be overriden - so it is not possible to make a method static and abstract at the same time.
With nested classes, the word static has a slightly different meaning than with methods. See Nested Classes for the details.