0

As I know that static and final combination can be used together but static and abstract or final and abstract combination is not allowed in java. But some where I have seen code like below

public class Enclosing {

    private static int x = 1;

    public static abstract class StaticNested {

        public abstract void run();
    }
  }

Can someone please explain the the concept and practical uses of this scenario.

  • 2
    Do you understand [what it means for a nested class to be static](https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)? Do you understand [what it means for a class to be abstract](https://stackoverflow.com/questions/1320745/abstract-class-in-java)? If you understand both those things then you'll understand the concept of an abstract static nested class. – Slaw Mar 23 '20 at 05:15
  • Short answer: No. Longer answer: No, because it would not be useful. – Bohemian Mar 23 '20 at 05:25
  • 2
    @Bohemian Static nested _classes_ certainly can be abstract and have the same valid uses as top-level abstract classes. That said, static _methods_ cannot be abstract, but the OP used a class as an example and not a method. – Slaw Mar 23 '20 at 05:28
  • @Slaw Hmmm. Quite so. – Bohemian Mar 23 '20 at 06:24

1 Answers1

2

static and abstract can be used together for nested classes, eg

class A {
    static abstract class B {
        abstract void someMethod();
    }
}

B is a static nested class that requires concrete subclasses to implement someMethod().

but cannot be used together for methods, eg

static abstract void someMethod();  // compile error

Of course abstract and final can't be used together - such a combination would be nonsensical.

Uses of static abstract class

A suitable use of a static abstract class would be for node-like objects that only your class uses, for example a class the represent an entry in a map (eg java.util.Map.Entry which is an interface, but could have been a static abstract class), or a class for a node of a LinkedList, but one where the implementation is left to implementatioons of the enclosing class that themselves also use and provide implementations of the nested class by extending it too.

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722