Tutorial by Examples

public class IntStack { private IntStackNode head; // IntStackNode is the inner class of the class IntStack // Each instance of this inner class functions as one link in the // Overall stack that it helps to represent private static class IntStackNode { privat...
When creating a nested class, you face a choice of having that nested class static: public class OuterClass1 { private static class StaticNestedClass { } } Or non-static: public class OuterClass2 { private class NestedClass { } } At its core, static nested c...
A full explanation of Access Modifiers in Java can be found here. But how do they interact with Inner classes? public, as usual, gives unrestricted access to any scope able to access the type. public class OuterClass { public class InnerClass { public int x = 5; } p...
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 abl...
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: public...
The reference to the outer class uses the class name and this public class OuterClass { public class InnerClass { public void method() { System.out.println("I can access my enclosing class: " + OuterClass.this); } } } You can access fields and ...
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 new operator only needs to be called on an instance of the outer cla...

Page 1 of 1