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
320
votes
18 answers

How are Anonymous inner classes used in Java?

What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of Java?
Warrior
  • 39,156
  • 44
  • 139
  • 214
215
votes
2 answers

Keyword for the outer class from an anonymous inner class

In the following snippet: public class a { public void otherMethod(){} public void doStuff(String str, InnerClass b){} public void method(a){ doStuff("asd", new InnerClass(){ public void…
shsteimer
  • 28,436
  • 30
  • 79
  • 95
43
votes
3 answers

What is this type of method overriding called in Java?

I'm relatively new to Java and I'm using a new API. I came across this method override and I'm not sure what this is called: public void exampleMethod() { Button loginButton = new Button("login"){ public void onSubmit(){ …
user186984
33
votes
8 answers

Is an enum constant-specific class body static or non-static?

I have a enum type class: public enum Operation { PLUS() { @Override double apply(double x, double y) { // ERROR: Cannot make a static reference // to the non-static method printMe()... …
user842225
  • 5,445
  • 15
  • 69
  • 119
33
votes
7 answers

Difference between new Test() and new Test() { }

What is the difference between these two ways of instantiating new objects of a class as follows: Test t1=new Test(); Test t2=new Test(){ }; When I tried the following code, I could see that both objects could access the method foo(), but t2 cannot…
Crusaderpyro
  • 2,163
  • 5
  • 29
  • 53
32
votes
4 answers

Declaring a method when creating an object

Why first way is correct, but second isn't? First way: new Object() { public void a() { /*code*/ } }.a(); Second way: Object object = new Object() { public void a() { /*code*/ } }; object.a(); And where can I…
25
votes
5 answers

Is cyclic dependency between anonymous class and parent class wrong?

I have following snippet of code: public class Example { private Integer threshold; private Map history; protected void activate(ComponentContext ctx) { this.history = Collections.synchronizedMap(new LinkedHashMap
25
votes
1 answer

Lambda behaving differently than anonymous inner class

While doing some basic lambda exercises, the output from an apparently identical anonymous inner class was giving me a different output than the lambda. interface Supplier { T get(T t); } Scenario #1 Supplier s1 = new…
victorantunes
  • 1,141
  • 9
  • 20
23
votes
3 answers

Anonymous inner classes in C#

I'm in the process of writing a C# Wicket implementation in order to deepen my understanding of C# and Wicket. One of the issues we're running into is that Wicket makes heavy use of anonymous inner classes, and C# has no anonymous inner…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
22
votes
3 answers

Java lambdas have different variable requirements than anonymous inner classes

I have an anonymous inner class and an equivalent lambda. Why are the variable initialization rules stricter for the lambda, and is there a solution cleaner than an anonymous inner class or initializing it in the constructor? import…
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
21
votes
4 answers

Constructors in Inner classes (implementing Interfaces)

How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this: JButton b = new JButton(new…
thepandaatemyface
  • 5,034
  • 6
  • 25
  • 30
15
votes
3 answers

access variables of outer class in Java

in Java android application how can i access variables of outer class from the inner anonymous class ? Example: ProgressDialog dialog = new ProgressDialog(this); ..... send.setOnClickListener(new View.OnClickListener() { …
user1246620
  • 151
  • 1
  • 1
  • 3
15
votes
1 answer

How can I find an anonymous inner class in Eclipse given only its synthetic name (Class$N)?

How do I find an anonymous inner class if I have only been given the name of the class Class$N when using Eclipse, without going through the code and counting each anonymous class? Is there a 'jump to anonymous class declaration' feature where I can…
user515655
  • 989
  • 2
  • 10
  • 24
13
votes
2 answers

re More than one instance of an anonymous inner class

This is in relation to my answer to a question provided in this thread: Are Inner Classes lightweight? I remember from my reading that if you can only create one object from a single anonymous inner class, and for this reason, if you want to say…
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
13
votes
4 answers

Groovy - closures vs methods - the difference

If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method?…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
1
2 3
16 17