A static inner class is a class nested inside another class that has the static
modifier. It's pretty much identical to a top-level class, except it has access to the private members of the class it's defined inside of.
class Outer {
private static int x;
static class Inner1 {
}
class Inner2 {
}
}
Class Inner1
is a static inner class. Class Inner2
is an inner class that's not static. The difference between the two is that instances of the non-static inner class are permanently attached to an instance of Outer
-- you can't create an Inner2
without an Outer
. You can create Inner1
object independently, though.
Code in Outer
, Inner1
and Inner2
can all access x; no other code will be allowed to.