понеділок, 2 лютого 2015 р.

Java: Nested classes

In Java there inner classes and nested classes. Here we will talk about nested classes.



Nested class is a class wich is declared in the another class:

class OuterClass { 
    ... 
    static class StaticNestedClass { 
        ... 
    } 

    class InnerClass { 
        ... 
    } 
    ...  }   
Nested classes can be static and non-static. Non-static classes can be called as inner classes.

Static nested classes do not have access to non-static fields and methods of outer class, which in some ways is similar to the static methods declared within a class. Access to non-static fields and methods can only be done through a reference to the instance of the outer class. In this regard, static nested classes are very similar to any other top-level classes.

In addition, static nested classes have access to any external static class methods, including a private.

Use of these classes is mainly in logical groupings of entities to improve the encapsulation and saving class-space.

Немає коментарів:

Дописати коментар