There is no top level static class.
You can only create static nested class in Java. And it is treated as a top-level class.
Since it's a static member of enclosing class, it can access all static members in enclosing class.
Defined like this:
public class OuterClass{
public static class StaticNestedClass{
//variable
//method
}
public class InnerClass{
//access even private members of OuterClass
}
}
Name of Inner class was given to non-static class of OuterClass.
Local class is the one defined inside method.
Anonymous class is the one created from interface.
Instantiate static nested class. Each instance is itself a single instance. Do not be fulled by static keyword here. static here means this is a static member of OuterClass, when using it, it is treated as top-level class.
OutClass.StaticNestedClass snc1 = new OutClass.StaticNestedClass();
OutClass.StaticNestedClass snc2 = new OutClass.StaticNestedClass();
Inner class has to be instantiated from instance of OuterClass.
OuterClass oc = new OuterClass();
OuterClass.InnerClass ocic = oc.new OuterClass.InnerClass();
Instantiate anonymous class
Interface Service {
//some code
}
Service s = new Service(){
//code to implement inteface methods
}
No comments:
Post a Comment