I have read that I can define a class as static
, but I don't understand why I might need to do so. What good are static classes? When might I need to开发者_StackOverflow中文版 use them? I would appreciate examples.
The static
keyword for classes is only allowed for nested classes, inside another class.
The difference between nonstatic nested classes (also known as inner classes) and static nested classes is that objects of the first ones always have a corresponding "outer" object, while objects of the latter ones don't (they only have private-level access, and are inside the namespace).
Here an example:
class Outer {
static class StaticNested {}
class Inner {}
}
Now we can create objects as following:
Outer o = new Outer();
Outer.StaticNested sn = new Outer.StaticNested();
Outer.Inner i = o.new Inner();
o
ist the corresponding Outer object to i
, while sn
has no such element. (Normally you'll create objects of inner classes from methods of the outer objects, there you can simply write new Inner()
and it takes this
as the outer object.)
From outside of Inner
we have no way to get the corresponding outer object, but inside it we can write Outer.this
to reference the outer object. This works over several levels, if needed.
You can define an inner class as static like this (to be thorough, the class will become a static nested class) :
class A {
public static class Inner { }
}
This means the class Inner
is somehow related to A
, but not attached to a particular instance. It is mainly a way to create some sort of relation between the two.
If you're a Java beginner, I suggest you don't lose too much time trying to find some useful application for this. Just remember that this possibility exists, and when you'll have a better understanding of object oriented programming in general and Java in particular, you'll come around this again.
It is important to note that only inner classes can be defined as static, "normal" classes cannot.
From the Sun Certified Programmer for Java 6 Study Guide:
A static nested class is simply a class that's a static member of the enclosing class.... The class itself isn't really "static"; there's no such thing as a static class. The
static
modifier in this case says that the nested class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.
The obvious next question is "What does static
mean?" All it means is that you get one per class instead of one per object. In other words, there's one version that everyone shares, not tied to any specific instance, instead of one per instance of the class). It's going to sound obvious by this point, but you should use a static class in situations where you want to use a static member and you also want to use a class. Static classes can also sometimes be helpful as containers for small helper methods, since they have access to their enclosing classes' private members.
Here's an example I remember from school:
public class Shapes {
List<Shape> shapes;
private static class ShapeSorter implements Comparator {
...
}
...
Note that you can't have a top-level static class; there's no enclosing class for it to be a member of. You also can't have a static class declared inside a method, because of scoping problems.
There are hundreds of other questions on Stack Overflow alone about what static means in Java. Here are a few to get you going:
- What is "static"?
- Why are you not able to declare a class as static in Java?
- Java: Static Class?
- Static nested class in Java, why?
精彩评论