Latest Questions tagged Inner Classes

Browse all latest questions tagged Inner Classes

Published 17 Sep, 2022
Java - Java Inner Classes Design Benefits

In java, you can have inner classes. I'm curious from a design perspective if there are any benefits to this. My initial thoughts are that having a separate file with the class is cleaner in separat...

Published 24 Aug, 2022
Java - 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 c...

Published 10 Sep, 2022
Java - How to access fields declared inside anonymous object?

Java lets you declare new fields inside anonymous classes, but I can't figure out how to access them from outside, even setting them to public doesn't let me. class A { public static void main(Str...

Published 03 Sep, 2022
Java - Inner classes in Java - Non static variable error

I'm still new to Java and I tried to create an inner class and call the method inside main. There is a compilation error saying Non static variable - This cannot be referenced from a static context...

Published 02 Sep, 2022
Java - Why does Java prohibit static fields in inner classes?

class OuterClass { class InnerClass { static int i = 100; // compile error static void f() { } // compile error } } Although it's not possible to access the static field with OuterClass.Inner...

Published 28 Aug, 2022
Java - Can we create an instance of an interface in Java?

Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main...

Published 30 Apr, 2022
Python - How to write inner class test method in Python with Unittest

In Java when doing unit testing it is common to have a test class that contains multiple inner classes for each method of the class to test. Each inner class can have multiple testing methods to test...

Published 24 Aug, 2022
Java - Java: calling outer class method in anonymous inner class

Recently, I ran into a mysterious problem in an android project, which I described here. I somehow solved the problem, but still don't know the exact reason behind it. Let's say I want to call a fun...

Published 20 Aug, 2022
Java - Why can't we have static method in a (non-static) inner class (pre-Java 16)?

Why can't we have static method in a non-static inner class? public class Foo { class Bar { static void method() {} // Compiler error } } If I make the inner class static it works. Wh...

Published 19 Aug, 2022
Java - How can I include raw JSON in an object using Jackson?

I am trying to include raw JSON inside a Java object when the object is (de)serialized using Jackson. In order to test this functionality, I wrote the following test: public static class Pojo { p...

Published 17 Aug, 2022
Java - How to instantiate inner classes in one step in Scala?

Consider this code: class Outer { class Inner } In Java it would be possible to create an instance of Inner with: Outer.Inner inner = new Outer().new Inner(); I know I can write this in Scala:...

Published 16 Aug, 2022
Java - Java inner class and static nested class

What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?

Published 15 Aug, 2022
Java - How to ignore inner/nested classes with JaCoCo?

I'm trying to ignore some generated classes, and the classes get ignored fine. But if those classes have inner classes, those classes still get included, despite the parent class being excluded. This...

Published 15 Aug, 2022
Java - When to use inner classes in Java for helper classes

If I have for example a class along with a helper class to do some of its functionality, does it make sense to make it as an inner class. public class Foo { private FooHelper helper;...

Published 13 Aug, 2022
Java - Is not an enclosing class Java

I'm trying to make a Tetris game and I'm getting the compiler error Shape is not an enclosing class when I try to create an object public class Test { public static void main(String[] args...