Accessing outer class members from within an inner class extending the outer class itself

The method getValue() and the field value are both private. As such, they are not accessible to any other classes, including sub-classes, ie. they are not inherited. In InnerClass#showValue() public void showValue() { System.out.println(getValue()); System.out.println(value); } because of the fact that those are private, getValue() and value are referring to the outer class’ members, which … Read more

Java: calling outer class method in anonymous inner class

The latter is more explicit and will allow you to call the outer class method if one exists in the inner class with the same name. class OuterClass { void foo() { System.out.println(“Outer foo”); } View.OnClickListener mListener1 = new View.OnClickListener() { void foo() { System.out.println(“Inner foo”); } @Override public void onClick(View view) { foo(); //Calls … Read more

Usage of inner class

Sometimes there is some functionality which is best represented as an object, but which is only meaningful within the context of another object, which does not necessarily need to be exposed to the outside world, and which can benefit from having access to the parent classes data (so as to not violate encapsulation). The best … Read more

Accessing outer class variable in inner class

Assuming your outer class is called Outer, from the scope of the inner class(non-static), Outer.this.foo to get at the field. For example, Outer.this.foo=new ArrayList<>(); where Outer is the name of the class and foo identifies the field. You can also grab it directly as foo=new Baz() but it’ll pick the inner field if there’s a … Read more

Test cases in inner classes with JUnit

You should annontate your class with @RunWith(Enclosed.class), and like others said, declare the inner classes as static: @RunWith(Enclosed.class) public class DogTests { public static class BarkTests { @Test public void quietBark_IsAtLeastAudible() { } @Test public void loudBark_ScaresAveragePerson() { } } public static class EatTests { @Test public void normalFood_IsEaten() { } @Test public void badFood_ThrowsFit() … Read more

Nested or Inner Class in PHP

Intro: Nested classes relate to other classes a little differently than outer classes. Taking Java as an example: Non-static nested classes have access to other members of the enclosing class, even if they are declared private. Also, non-static nested classes require an instance of the parent class to be instantiated. OuterClass outerObj = new OuterClass(arguments); … Read more