Questions tagged [anonymous-class]

An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator.

An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call. When a local class is used only once, consider using anonymous class syntax, which places the definition and use of the class in exactly the same place.

699 questions
6
votes
3 answers

Allman-style anonymous classes

Any recommendations on how to use anonymous classes while staying consistent with Allman indent style? I don't really like anything I've come up with, e.g. // Pass as parameter. foo(new Clazz( ) { // Do stuff. }); // Assign to…
gdejohn
  • 7,451
  • 1
  • 33
  • 49
6
votes
1 answer

lambda vs anonymous class

I have the following piece of code: eventBus.subscribe(new EventBusListener() { @Override public void onEvent(Event event) { event.getPayload(); } }); eventBus.subscribe(new…
louis amoros
  • 2,418
  • 3
  • 19
  • 40
6
votes
1 answer

Using anonymous runnable class code goes in deadlock state but with lambda it works fine

I am trying to find out the cause behind below mentioned code. Here if I create Thread using anonymous inner class it goes into deadlock state but with lambda expressions it works fine. I tried to find the reason behind this behavior but I could…
cody123
  • 2,040
  • 24
  • 29
6
votes
1 answer

In which version of Java were anonymous classes introduced?

The purpose of anonymous classes is clear and understandable. But I'm unable to find the minimum version of Java which supports anonymous classes. Can anyone tell me the exact Java version which supports it?
6
votes
7 answers

Accessing inner anonymous class members

Is there any way other than using reflection to access the members of a anonymous inner class?
Saravanan M
  • 4,697
  • 5
  • 35
  • 37
6
votes
4 answers

Can anybody explain the working of following code...?

Can anybody explain the working of following code...? interface myInterface{} public class Main { public static void main(String[] args) { System.out.println(new myInterface(){public String toString(){return…
Siddhi
  • 63
  • 6
6
votes
4 answers

Implement Interface with Anonymous Class in C++

In java we can implement an interface with Anonymous class like this : import java.util.function.Predicate; public class Test { public static void main(String[] args) { System.out.println(testIf("", new Predicate() { …
FaNaJ
  • 1,329
  • 1
  • 16
  • 39
6
votes
1 answer

golang anonymous field of type map

I thought I'd be able to make an ordered map type by using anonymous fields: type customMap struct{ map[string]string ordered []string } where I could reference the map with customMapInstance["key"] and iterate over ordered. Alas, it…
Ethan
  • 1,057
  • 1
  • 14
  • 23
6
votes
1 answer

Android Studio / IntelliJ Anonymous Class Formatting

I was wondering if it is possible to change the auto-formatting in Android Studio in such a way that braces for anonymous classes are placed on the same line while still putting braces for regular classes, methods and blocks on a new…
6
votes
3 answers

Final field and anonymous class

I'm still not satisfied with explanation regarding anonymous class and final field. There were tons of questions trying to explain obvious problem but I have not found answers for all my questions :-) Suppose following code: public void method(final…
Martin Podval
  • 1,097
  • 1
  • 7
  • 16
6
votes
3 answers

What exactly is happening in this Java code snippet?

Here is the code: timer.schedule(new TimerTask() { public void run() { synchronized(this) { try { // System.out.println(" ITERATION = "); …
Flame of udun
  • 2,136
  • 7
  • 35
  • 79
6
votes
1 answer

Private inner class synthesizes unexpected anonymous class

When you compile a Java class with a private inner class, it appears that an anonymous class is automatically synthesized along with it for some reason. This class is sufficient to reproduce it: public class SynthesizeAnonymous { public static…
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
6
votes
2 answers

Best practice for inner/anonymous classes

What's the best practise(design and performance wise) for anonymous classes and static inner classes? Personally I would like to think that static inner classes provide better encapsulation and should give better performance as they dont have…
David
  • 375
  • 4
  • 11
5
votes
6 answers

Java anonymous class that implements ActionListener?

I was recently doing a programming assignment that required us to implement in code a program specified by a UML diagram. At one point, the diagram specified that I had to create an anonymous JButton that displayed a count (starting at one) and…
Tim
  • 59,527
  • 19
  • 156
  • 165
5
votes
4 answers

Why does my lambda get illegal forward reference, but my anonymous class does not?

I am trying to represent a State Transition Diagram, and I want to do this with a Java enum. I am well aware that there are many other ways to accomplish this with Map or maybe with a static initialization block in my enum. However, I am…
davidalayachew
  • 1,279
  • 1
  • 11
  • 22