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
14
votes
3 answers

Anonymous class implementing interface

I have the following code inside a method: var list = new[] { new { Name = "Red", IsSelected = true }, new { Name = "Green", IsSelected = false }, new { Name = "Blue", IsSelected = false }, }; I would like to call a function that…
flayn
  • 5,272
  • 4
  • 48
  • 69
14
votes
6 answers

When do I need anonymous class in C++?

There's a feature called anonymous class in C++. It's similar with anonymous struct in C. I think this feature is invented because of some needs, but I can't figure out what that is. Can I have some example which really needs anonymous class?
eonil
  • 83,476
  • 81
  • 317
  • 516
13
votes
1 answer

How are anonymous classes compiled in Java?

I've heard that Java bytecode actually doesn't support any kind of unnamed classes. How does javac translate unnamned classes to named ones?
keiter
  • 3,534
  • 28
  • 38
13
votes
1 answer

Proguard removes Anonymous class in Anonymous class in Java

I have a issue that Proguard is not working when you declare a anonymous class within a anonymous class. This is what it looks like in a basic example: public class Class1 { public void function1 (){ new Class2(){ @Override …
Arne Fischer
  • 922
  • 6
  • 27
13
votes
3 answers

When to use anonymous classes?

I run into some code which contains anonymous classes. I haven't met with anonymous classes before, so I did some research on them. My main area of interest is java, so I checked Oracle's tutorial of anonymous classes. I understand the mechanism and…
Tamas G.
  • 677
  • 3
  • 21
  • 38
13
votes
3 answers

Why is an anonymous class in a static context valid

I have a misunderstanding about what an anonymous class in Java is. Consider the following simple example: public static void main (String[] args) throws java.lang.Exception { B b = new B(){ }; System.out.println(b.b); } interface B{ int b…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
13
votes
2 answers

Reference to Public Enum results in Anonymous Class

I'm getting an anonymous class at compile-time that I'm not expecting. Relevant code follows, then a more detailed explanation: Entirety of CircuitType.java: public enum CircuitType { V110A20, V110A30, V208A20, V208A30 } From Auditor.java, lines…
Maarx
  • 633
  • 1
  • 7
  • 18
12
votes
4 answers

How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?

I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream: public static final FilterFactory BZIP2_FACTORY = new FilterFactory() { public InputStream makeFilter(InputStream in) { // a lot of other…
Zack
  • 6,232
  • 8
  • 38
  • 68
12
votes
2 answers

What's a good example for making use of an anonymous class in PHP7

As I looked for the new PHP7-features I stumbled upon anonymous classes. I didn't understand when they should become useful, and looked for an example. I read this article, but I don't see the benefits of this feature. In the last section before the…
12
votes
1 answer

Value of "this" in an anonymous class vs a lambda expression

I am little bit confused with the different behavior of an anonymous class and a lambda expression. When I'm using a lambda expression: //Test.java Runnable r1 = () -> System.out.println(this); Runnable r2 = () ->…
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
12
votes
3 answers

Where is final parameter stored in anonymous class instance?

I have the following static factory method that creates a list view out of an int array: public static List newInstance(final int[] numbers) { return new AbstractList() { @Override public Integer get(int index)…
Kewei Shang
  • 949
  • 3
  • 11
  • 27
12
votes
3 answers

Virtual tables on anonymous classes

I have something similar to this in my code: #include #include struct Base { virtual int Virtual() = 0; }; struct Child { struct : public Base { virtual int Virtual() { return 1; } } First; struct : public Base …
GhassanPL
  • 2,679
  • 5
  • 32
  • 40
12
votes
4 answers

How to dynamically create an anonymous class that extends an abstract class in PHP?

Simply, let me explain by an example. start()); ?> This is from PHP manual. I am wondering if there is a way to do this in more…
jlai
  • 909
  • 1
  • 10
  • 17
11
votes
3 answers

Two double nested anonymous inner classes. How to get 1st level anonymous class members?

Inner class is Adapter, inner-inner class is Listener. How to access (obscured) Adapter members/methods from Listener? list.setAdapter(new Adapter() { public View getView() { // ... button.setListener(new Listener() { public void…
Chloe
  • 25,162
  • 40
  • 190
  • 357
11
votes
1 answer

How to add @SerialVersionUID to an anonymous class?

I want to translate the following piece of code from Java to Scala: Foo foo = new Foo() { private static final long serialVersionUID = 12345L; } Class Foo is an abstract class. How does the equivalent code look like in Scala?
soc
  • 27,983
  • 20
  • 111
  • 215