Questions tagged [anonymous-inner-class]

Anonymous Inner Classes are local classes that are declared and instantiated inline.

These are local classes that are declared and instantiated inline, often in the middle of an expression or as an argument of a method. They can only directly extend one class or implement one interface. They can specify arguments to the constructor of the superclass, but cannot otherwise have a constructor (however, this is not a limitation, since it can have an instance initializer block to perform any initialization).

246 questions
6
votes
3 answers

How to use inner class in practical

Why would someone use an inner class? The same functionality can be achieved with a local class or subclass. An example would also be appreciated.
Birju088
  • 93
  • 1
  • 7
6
votes
3 answers

Java - Interface, instantiating an interface?

So I just found this code example online a while ago and I'm going over it again but quite confused. From looking at it, what I gather (and it might be wrong) is that it passes to the print method in the NumberPrinter class a Printer object.…
mino
  • 6,978
  • 21
  • 62
  • 75
5
votes
2 answers

How can I pass a non final variable to an anonymous inner class?

I have these lines of code. I know you can not pass a non final variable to an inner class but I need to pass the variable i to the anonymous inner class to be used as a seatingID. Can you suggest ways of doing that ? JButton [] seats = new JButton…
dave
  • 419
  • 1
  • 5
  • 10
5
votes
5 answers

Accessing an outer anonymous class's field from an inner anonymous class

To access the field x of an outer class A from an inner class B, I realize that you can use "A.this.x". But what if the outer class is also anonymous? For example, public class Main1 { public static void main(String[] args) { Comparable…
Zoot101
  • 51
  • 2
5
votes
1 answer

Mystery of the Hidden Java Inner Class That Doesn't Exist

I'm working on a transcript project for school, and it's compiling as if there's an anonymous inner class, but I haven't written any. Why is javac compiling an inner class without there being any inner classes (including enums or exceptions)…
BrainFRZ
  • 437
  • 3
  • 15
5
votes
1 answer

Is there a syntax to get the reference to an anonymous inner class from a further anonymous inner class?

Consider this case: public class SomeClass { public void someMethod() { new SomeInterface() { public void someOtherMethod() { new SomeOtherInterface() { new someThirdMethod() { …
Yishai
  • 90,445
  • 31
  • 189
  • 263
5
votes
4 answers

Do I need to unregister 'anonymous' BroadcastReceiver

I recently asked a question about checking on the state of a sent SMS and the answer given was a code snippet which registered two 'anonymous inner' (please correct my terminology if it is incorrect) BroadcastReceivers to listen for SMS…
barry
  • 4,037
  • 6
  • 41
  • 68
4
votes
6 answers

Why can't I hold EnumMap entries via a "for" loop, even if I use "final"? Best workaround?

I'm having some strange behaviour. [UPDATE: Full runnable example given:] package finaltestwithenummapentry; import java.util.ArrayList; import java.util.EnumMap; import java.util.Map.Entry; public class FinalTestWithEnumMapEntry { enum…
Navigateur
  • 1,852
  • 3
  • 23
  • 39
4
votes
1 answer

Find anonymous classes by annotation

Is there a way to find anonymous classes by some annotation with some java reflection library like (Reflections)? I have this code: it use declare a inner class (extends Object) and annotated it with @DemoAnnotation public class DemoUsageService { …
Ralph
  • 118,862
  • 56
  • 287
  • 383
4
votes
1 answer

Java anonymous inner class calling enclosing type super types method

I give you an example to set some context here, so I have two interfaces each inheriting the same parent interface and defining their own implementation of the parent interface's abstract method. interface A { Set a(); } interface B extends…
4
votes
1 answer

Why does Java create anonymous class internally as static?

abstract class Person { abstract void eat(); } class TestAnonymousInner { public static void main(String args[]){ Person p=new Person() { void eat(){System.out.println("nice fruits");} }; p.eat(); } } Internal…
Rohan Patel
  • 1,758
  • 2
  • 21
  • 38
4
votes
0 answers

Netbeans autocomplete does not work inside a lambda expression

I have encountered a strange bug in Netbeans 8.2. When declaring an object with a lambda expression, autocomplete for variables inside the lambda does not seem to work, and instead I see global variables, suggestions for keywords amongst other…
Morgan
  • 907
  • 1
  • 13
  • 35
4
votes
4 answers

In Java how can I reference an anonymous inner class from within itself?

I'm defining a callback and would like to refer to the callback from within itself. The compiler does not like this and claims that the variable referring to the callback is not initialized. Here's the code: final Runnable callback = new Runnable()…
Sami Samhuri
  • 1,540
  • 17
  • 21
4
votes
2 answers

How to set conditional breakpoint in anonymous inner class depending on final local variable?

suppose I have the following class and want to set a conditional breakpoint on arg==null at the marked location. This won't work in eclipse and gives the error "conditional breakpoint has compilation error(s). Reason: arg cannot be resolved to a…
Axel
  • 13,939
  • 5
  • 50
  • 79
4
votes
3 answers

NullPointerException in anonymous inner class

I have a simple Android Fragment CreateAccountBookingFragment which inherits from CreateAccountFragment, that has the following method : @Override public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) { …
1 2
3
16 17