1

In java, An outer class may be public, final, default or abstract. Why not Static like

public static class MyClass{}

earthmover
  • 4,395
  • 10
  • 43
  • 74
  • 2
    What semantics would you expect a static class to have, as opposed to static members of a class? – Anders R. Bystrup Sep 11 '13 at 09:56
  • Because it's redundant and expresses nothing, and raises the false expectation that omitting the word 'static' changes the meaning when it doesn't. – user207421 Sep 11 '13 at 10:28
  • Please read the answer to your question, Why can't a Java class be declared as static? on http://stackoverflow.com/a/40015089/2078093 – Naresh Joshi Oct 13 '16 at 07:46

1 Answers1

4

An outer class is already implicitly static.

Non-static nested class (= inner class) means thats the inner class implicitly has a reference to its parent class.

That's why, for nested class, you can distinguish between static and non-static. This does not make sense for outer classes.

Here is an example to understand the difference between static/non-static nested class. You should understand why it does not make sense in an outer class.

public class MyClass {

  private String anAttributeOfMyClass;

  private /*static*/ class MyInnerClass {

    public void foo() {
      /*
       * Here, I can access the attribute of the parent class
       * because I implicitly have a reference to it.
       * Try to make the nested class static an see the difference.
       */
      anAttributeOfMyClass.trim();
    }
  }

}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • 1
    can you please elaborate? this is new for me – Prasad Kharkar Sep 11 '13 at 09:57
  • As you describe the notion it is anyway irrelevant to static class in other languages like C# which cannot be instantiated. – arjacsoh Sep 11 '13 at 10:02
  • an interface method is implicitly public and abstract, but even if we declare them as public and abstract, it is valid. Then why is it incorrect to declare an outer class as static if it is implicitly static? I am not sure I'm following this answer. – Prasad Kharkar Sep 11 '13 at 10:05
  • @Prasad True. And if you explicitly declare interface methods as public, Sonar will complain about it. But in java 8, you might be able to write complete methods in interfaces and I guess it will be useful to be able to choose the visibility. – Arnaud Denoyelle Sep 11 '13 at 10:11
  • @Prasad Same remark for abstract. In Java 8 interfaces, you will be able to choose between `abstract` and `default`. See http://blog.hartveld.com/2013/03/jdk-8-13-interface-default-method.html – Arnaud Denoyelle Sep 11 '13 at 10:15
  • @ArnaudDenoyelle, I guess I will have to study about Sonar and java 8. Can you please provide me some references about an outer class acting as static one ? – Prasad Kharkar Sep 11 '13 at 10:16
  • 'statc inner' is a contradiction in terms. Inner classes are non- static by definition. *Nested* classes can be static or non-static: the latter implies 'inner'. -1 for raising this confusion. – user207421 Sep 11 '13 at 10:31
  • @EJP I did not know the semantical difference between inner and nested class. I read a bit to understand and edited the answer. Thank you for explaining downvote. – Arnaud Denoyelle Sep 11 '13 at 11:26