1

I was having problems understanding the concept of static classes until I found this question

Just to be sure that I understood correctly: Is it right to assume that static nested classes are exactly the same thing as normal classes and the whole point of making a static nested class is just for grouping?

Foreign
  • 385
  • 2
  • 20

2 Answers2

2

For example, nested classes may be private or protected. That's already a huge difference.

ardenit
  • 3,610
  • 8
  • 16
  • Accidentally deleted my comment. So the only difference is that they can be `private`? – Foreign Jan 21 '20 at 02:31
  • 1
    Yes, that's the only difference. – ardenit Jan 21 '20 at 02:32
  • There are some differences to do with name resolution - for example, a nested class can shadow other classes of the same name, and is inherited by subclasses of the outer class, and a class can even inherit multiple classes of the same name (e.g. from two interfaces) in which case using the nested class's name may be ambiguous and result in a compilation error. There is also a difference in that an outer class's canonical name equals its binary name, but a nested class's binary name has the symbol `$` in it. – kaya3 Jan 21 '20 at 02:50
0

Here are some major differences.

  1. Nested classes may not access any enclosing class instance fields or methods unless they have an instance of the enclosing class. Inner classes may access those fields directly.

  2. Depending on the access level, nested static classes may be directly instantiated from outside the enclosing class.

   class A {
      static class B {
      }
    }

    A.B b = new A.B();

non static inner classes require an instance of the enclosing class before they can be instantiated.

   class A {
       class B {
       }
   }

   A a = new A();
   A.B b = a.new B();
  1. Static classes are good for grouping and creating inner objects which do not rely on instance fields of the parent class. One could view them as helper classes. Non static inner classes are good for creating listeners, iterators, etc which would probably depend on interacting directly with features of the enclosing class.
WJS
  • 36,363
  • 4
  • 24
  • 39
  • #1 is very badly phrased. A nested class (static or not) has **access** to all members of the outer class and all other nested classes, even `private` members. It will of course need an *instance* of those classes in order to access a non-static member. An inner class would implicitly have such an instance reference, but the **access-level** is the same regardless. The keywords `public`, `protected`, and `private` are called "access modifiers" for a reason, because they define "access control", regardless of whether the member is accessed using `this` an instance obtained by other means. – Andreas Jan 21 '20 at 03:39
  • To state it more clearly, "access" is about being "allowed" to get/set/call the member. It has nothing to do with how the instance/class declaring the member is obtained (`this`, reference variable, return value, ...). As [JLS 6.6. Access Control](https://docs.oracle.com/javase/specs/jls/se13/html/jls-6.html#jls-6.6) says it: *Accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers.* --- It is not about whether/how you have an instance available when trying to use a non-static member. – Andreas Jan 21 '20 at 03:45
  • 'Non-static inner' is double-talk. You are here enumerating differences between static and inner classes, not between static and normal classes. – user207421 Jan 21 '20 at 04:09