How does java allow the instantiation of inner static class

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am able to create instances of the static inner class without compilation error. What does this mean? How does java allow one to create objects for static class?.. Kindly help me with this.

public class StringMatrix {
    static class moves{
        int x;
        int y;
        moves(int x,int y){
            this.x=x;
            this.y = y;
        }
        static moves[] movements = {new moves(0,1),new moves(1,1),new moves(0,-1),new moves(1,0),new moves(-1,0),new moves(-1,-1),new moves(-1,1),new moves(1,-1)};
    }
}

Answers

Java is allow to create static inner class.

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

You can access static nested classes are accessed using the enclosing class name: like

StringMatrix.moves
  Why do we need static nested class?   

static nested classes aren t really needed. However, you could make the argument that if two classes are only used together -- such as class A uses helper class B, and B is never used independently -- then a static nested class is the way to go. In other words, by making B just a regular old class, the programmer who stumbles across it may try to use it. If B were a static nested class, then it s more clear that it has a special use that relates to A.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/22677450/how-does-java-allow-the-instantiation-of-inner-static-class

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils