aspose file tools*
The moose likes Beginning Java and the fly likes static and abstract inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static and abstract inner class" Watch "static and abstract inner class" New topic
Author

static and abstract inner class

Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27
If I write code like this:

Compile error message:
illegal combination of modifier: abstract and static

But inner class can combine modifier: abstract and static

Why?
Is there anyone explain me?
Thanx in advance!!
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
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.
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
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.
Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27
I still get confused!!
If I declare static inner class as abstract, why compile OK ??
Would anyone explain me clear??
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
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.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Is the above explantion for static abstract inner classes correct?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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?
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
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)





Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Hi Ogeh

Thanks What does packaging convenience imply in this case?


Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

A static method is a class-level method (see Understanding Instance and Class Members). Static methods are not polymorphic, which means you cannot override them in subclasses.

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static and abstract inner class