If you are from a C# background, you will know what I am talking about. In c# when a class is static, that means you cannot instantiate instance from this class. and beside, you cannot have instance method in the class definition.

 

however, static class in java is a bit different, the static class is used in java as nested class, there is no such outer static class in java.

 

so static class is in constrast to non-static nested class, where there is certain boundage from the nested class to its containing class. we will see an example below to show their difference.

 

 

Java代码
  1. /**
  2. *
  3. * @author boqwang
  4. *
  5. * a note on the static class in Java
  6. * - C# interprets static classes as abstract final
  7. * - java class can be either abstract or final, not both. To Prevent a class from being instantiated, one can declare a private constructor.
  8. */
  9.  
  10.  
  11. class OuterClass {
  12. public static class StaticNestedClass {
  13.  
  14. }
  15.  
  16. public class InnerClass {
  17.  
  18. }
  19.  
  20. public InnerClass getAnInnerClass () {
  21. return new InnerClass();
  22. // which is equivalent to
  23. // return this.new InnerClass();
  24. }
  25.  
  26. public static StaticNestedClass getAnClassStatically() {
  27. return new StaticNestedClass();
  28. }
  29.  
  30. public static StaticNestedClass getAnInnerClassStatically() {
  31. // this won't work, the instance to the OuterClass is needed.
  32. // return new InnerClass();
  33. return null;
  34. }
  35.  
  36.  
  37. }
  38.