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
12
votes
4 answers

Scala: abstract classes with anonymous types

I am reading "Scala for the Impatient" and in 8.8 they say: [..] you can use the abstract keyword to denote a class that cannot be instantiated [..] abstract class Person { val id: Int ; var name: String } And several lines later: You can…
Alina
  • 2,191
  • 3
  • 33
  • 68
12
votes
1 answer

Code behaves different after converting anonymous class to lambda

I'm trying to refactor this code to use a lambda instead of anonymous class. It's a simple list of items in a GUI. I register a different listener to each item, and the last item created does something special when clicked. class ItemList { …
Dog
  • 7,707
  • 8
  • 40
  • 74
11
votes
9 answers

Why can a lambda expression be used as a Comparator?

In the book OCP Study Guide there is this example about a Comparator that can be initialized in two ways. The first is via an anonymous class like this: Comparator byWeight = new Comparator(){ public int compare(Duck d1, Duck d2){ …
Maurice
  • 6,698
  • 9
  • 47
  • 104
10
votes
4 answers

Accessing variables from inner class

I've got some code which defines an anonymous inner class for a callback handler. This handler needs to assign a local variable, see below. I need to assign resp in the callback and refer to it towards the end of the function. I am getting this…
fred basset
  • 9,774
  • 28
  • 88
  • 138
10
votes
2 answers

Can you create anonymous inner classes in Swift?

I'm tired of declaring entire classes as having the ability to handle UIAlertView clicks by making them extend UIAlertViewDelegate. It starts to feel messy and wrong when I have multiple possible UIAlertViews, and have to distinguish which was…
9
votes
1 answer

LeakCanary reports leaked activity Instance by anonymous implementation of locationlistener

In onDestroy locationManager.removeUpdates(locationListener); locationListener = null; The Anonymous implementation locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (locationManager != null) { …
9
votes
2 answers

invoke methods with parameter(s) on anonymous inner class beans in EL

If I have an anonymous inner class object like this (where Foo is an interface): Foo foo = new Foo(){ @Override public String hello(Object dummyArg){ return "hello, world."; } }; and I try to call Foo.hello from a jsp like…
user316146
  • 1,307
  • 1
  • 12
  • 19
8
votes
4 answers

Does an anonymous inner class always capture a reference to "this" (outer) object when accessing its primitives etc.?

If I have [EDIT: added the type definition for "Inner"] interface Inner{ public void execute(); } class Outer{ int outerInt; public void hello(){ Inner inner = new Inner(){ public void execute(){ …
Navigateur
  • 1,852
  • 3
  • 23
  • 39
8
votes
1 answer

What does $$ in javac generated name mean?

When shifting through java call graph generated by libraries like DependencyFinder and java-callgraph, I found out that java compiler generate names for anonymous functions, inner classes, etc. I've found out the meaning of a couple of them (please…
rickchristie
  • 1,640
  • 1
  • 17
  • 29
7
votes
3 answers

Kotlin, how to assign callback implementation to a variable

I'm trying to assign a callback implementation of an interface (defined inside a class A) to a variabile defined inside another class B. Let's say that class A has the interface OnSomethingHappens which defines a doSomething method. Inside class B…
user9257465
  • 91
  • 1
  • 1
  • 6
6
votes
2 answers

What is the access type of an anonymous inner class?

I've been thinking about classes, and specifically about anonymous inner classes. This led me to wonder what is the access type of an anonymous inner class? I realize in most cases this won't change anything but it might make a difference for…
Tim B
  • 40,716
  • 16
  • 83
  • 128
6
votes
2 answers

Anonymous class in static method, holds reference to what?

Afaik, in Java an anonymous inner class always holds a reference to the enclosing instance of its outer class. Now what happens when I put an anonymous class inside a static method? As there is no object of its outer class, does it hold a reference…
6
votes
3 answers

Java - changing the value of a final variable from within a lambda

In Java I have the following code List myList = new ArrayList<>(); for (int i=0;i<9;i++) { myList.add(i); } Integer sum = 0; myList.forEach(i -> { sum = sum + i; // does not compile, sum needs to be final or effectively final });…
AlexGuevara
  • 932
  • 11
  • 28
6
votes
5 answers

What does "Code as Data" mean?

I recently came across a presentation from EclipseCon 2014, where on page 5 they say "Lambda expressions allow you to treat code as data". I also came across this example code button.addActionListener(new ActionListener() { public void…
SDS
  • 828
  • 3
  • 17
  • 35
6
votes
2 answers

Anonymous Inner Classes Inside Methods

Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{ public static List modifiedList(final List list){ return new ArrayList(){ @Override public…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
1
2
3
16 17