| Start |
|---|
A Simple Stack Using a Nested Class And the use thereof, which (notably) does not at all acknowledge the existence of the nested class. |
| Static vs Non Static Nested Classes When creating a nested class, you face a choice of having that nested class static: Or non-static: At its core, static nested classes do not have a surrounding instance of the outer class, whereas non-static nested classes do. This affects both where/when one is allowed to instantiate a nested class, and what instances of those nested classes are allowed to access. Adding to the above example: Thus, your decision of static vs non-static mainly depends on whether or not you need to be able to directly access fields and methods of the outer class, though it also has consequences for when and where you can construct the nested class. As a rule of thumb, make your nested classes static unless you need to access fields and methods of the outer class. Similar to making your fields private unless you need them public, this decreases the visibility available to the nested class (by not allowing access to an outer instance), reducing the likelihood of error. |
| Access Modifiers for Inner Classes A full explanation of Access Modifiers in Java can be found here (opens new window). But how do they interact with Inner classes?
both
The Inner Class itself can have a visibility other than |
| Anonymous Inner Classes An anonymous inner class is a form of inner class that is declared and instantiated with a single statement. As a consequence, there is no name for the class that can be used elsewhere in the program; i.e. it is anonymous. Anonymous classes are typically used in situations where you need to be able to create a light-weight class to be passed as a parameter. This is typically done with an interface. For example: This anonymous class defines a Other interfaces that are frequently implemented and instantiated using anonymous classes are Anonymous inner classes can also be based on classes. In this case, the anonymous class implicitly ConstructorsAn anonymous class cannot have an explicit constructor. Instead, an implicit constructor is defined that uses The implicit constructor for our anonymous subclass of Naturally, this does not work when extending an interface. When you create an anonymous class from an interface, the classes superclass is |
| Create instance of non-static inner class from outside An inner class which is visible to any outside class can be created from this class as well. The inner class depends on the outside class and requires a reference to an instance of it. To create an instance of the inner class, the Note the usage as |
| Method Local Inner Classes A class written within a method called method local inner class. In that case the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined. The example of using method local inner class: Executing will give an output: |
| Accessing the outer class from a non-static inner class The reference to the outer class uses the class name and You can access fields and methods of the outer class directly. But in case of name collision you can use the outer class reference. Syntax
RemarksTerminology and classificationThe Java Language Specification (JLS) classifies the different kinds of Java class as follows: A top level class is a class that is not a nested class. A nested class is any class whose declaration occurs within the body of another class or interface. An inner class is a nested class that is not explicitly or implicitly declared static. An inner class may be a non-static member class, a local class, or an anonymous class. A member class of an interface is implicitly static so is never considered to be an inner class. In practice programmers refer to a top level class that contains an inner class as the "outer class". Also, there is a tendency to use "nested class" to refer to only to (explicitly or implicitly) static nested classes. Note that there is a close relationship between anonymous inner classes and the lambdas, but lambdas are classes. Semantic differences
This makes static nested classes useful when you need to represent multiple "entity types" within a tight abstraction boundary; e.g. when the nested classes are used to hide "implementation details". Inner classes add the ability to access non-static variables declared in enclosing scopes: The fact that an inner class instance can refer to variables in a enclosing class instance has implications for instantiation. Specifically, an enclosing instance must be provided, either implicitly or explicitly, when an instance of an inner class is created. |
Read Article
https://develop.horje.com/learn/1434/reference