Why can outer Java classes access inner class private members?

I observed that Outer classes can access inner classes private instance variables. How is this possible? Here is a sample code demonstrating the same: class ABC{ class XYZ{ private int x=10; } public static void main(String… args){ ABC.XYZ xx = new ABC().new XYZ(); System.out.println(“Hello :: “+xx.x); ///Why is this allowed?? } } Why is this … Read more

When exactly is it leak safe to use (anonymous) inner classes?

I have been reading some articles on memory leaks in Android and watched this interesting video from Google I/O on the subject. Still, I don’t fully understand the concept, and especially when it is safe or dangerous to user inner classes inside an Activity. This is what I understood: A memory leak will occur if … Read more

What causes error “No enclosing instance of type Foo is accessible” and how do I fix it?

I have the following code: class Hello { class Thing { public int size; Thing() { size = 0; } } public static void main(String[] args) { Thing thing1 = new Thing(); System.out.println(“Hello, World!”); } } I know Thing does nothing, but my Hello, World program compiles just fine without it. It’s only my defined … Read more

Difference between final and effectively final

I’m playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know that when I use variables inside anonymous class they must be final in outer class, but still – what is the difference between final and effectively final? 14 … Read more