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

In Java, can an anonymous class declare its own type parameters?

Can an anonymous class declare its own type parameters?
John Assymptoth
  • 8,227
  • 12
  • 49
  • 68
11
votes
2 answers

Is Java "caching" anonymous classes?

Consider the following code: for(int i = 0;i < 200;i++) { ArrayList currentList = new ArrayList() {{ add(i); }}; // do something with currentList } How will Java treat the class of currentList? Will it consider it a…
Geo
  • 93,257
  • 117
  • 344
  • 520
11
votes
3 answers

Which part of JLS said anonymous classes cannot have public/protected/private member classes

Consider this piece of code: public class TopLevelClass { Cloneable c = new Cloneable() { private int privateField; private void privateMethod() {}; }; } There is an anonymous class that has a private member field and a…
johnchen902
  • 9,531
  • 1
  • 27
  • 69
10
votes
3 answers

Java, anonymous inner class definition

I've seen a couple of examples similar to this in Java, and am hoping someone can explain what is happening. It seems like a new class can be defined inline, which seems really weird to me. The first printout line is expected, since it is simply…
lots_of_questions
  • 1,109
  • 3
  • 16
  • 24
10
votes
1 answer

PHP 7 - Comparing Anonymous Class Instances

I have tried this code: $ac1 = new class {}; $ac2 = new class {}; var_dump($ac1); // object(class@anonymous)#1 (0) {} var_dump($ac2); // object(class@anonymous)#2 (0) {} var_dump(new class {}); // object(class@anonymous)#3 (0) {} var_dump($ac1 ==…
Rax Weber
  • 3,730
  • 19
  • 30
10
votes
3 answers

Statements in anonymous class declaration

I was going through anonymous class tutorial from oracle documentation (https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html) I've copied the code used in the tutorial.(Comments Statement1 and Statement2 are appended by…
pratz
  • 107
  • 7
10
votes
1 answer

Is Java lambda anonymous object reused?

As of the current JDK 1.8 implementation, it builds an anonymous object to hold the lambda function and calls the function on such object. Is this anonymous object reused in each call, or is an object re-created each time?
Alex Suo
  • 2,977
  • 1
  • 14
  • 22
10
votes
1 answer

Creating anonymous class as custom key in dictionary

While using dictionary, i always override GetHashCode and Equals ( or provide a custom comparer to the dictionary). What happens behind the covers when i create an anonymous class as key? Sample Code.... var groups=(from item in items …
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
10
votes
1 answer

How do I simulate anonymous classes in C#

I'm writing a small data structures library in C#, and I'm running into an architectural problem. Essentially I have a class which implements the visitor pattern, and there are many possible implementations of visitors: public interface…
Juliet
  • 80,494
  • 45
  • 196
  • 228
9
votes
3 answers

Singletons, Enums and anonymous inner classes

As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle... Thus what about an Enum with multiple instances? Can…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
9
votes
2 answers

How to detect an instance of an anonymous class?

Since PHP7, we have anonymous classes. How can we know if an $instance is an instance of an anonymous class?
wpclevel
  • 2,451
  • 3
  • 14
  • 33
9
votes
5 answers

Java: Anonymous inner class using a local variable

How can I get the value of userId passed to this method in my anonymous inner subclass here? public void doStuff(String userID) { doOtherStuff(userID, new SuccessDelegate() { @Override public void onSuccess() { …
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
9
votes
6 answers

Is it possible to serialize anonymous class without outer class?

I made a small research on web and reviewed related topics on this site, but the answers were contradictory: some people said it is not possible, others said it is possible, but dangerous. The goal is to pass an object of the anonymous class as a…
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
9
votes
2 answers

C# equivalent of creating anonymous class that implements an interface

I've recently started using C#, and I wanted to find an equivalent method to this. I do not know what this is called, so I will simply show you by code. With Java, I was able to create an interface like so: public interface Event { public void…
user1606034
  • 113
  • 1
  • 5
9
votes
3 answers

Anonymous code block in anonymous class Java

Possible Duplicate: What is Double Brace initialization in Java? While looking at some legacy code I came across something very confusing: public class A{ public A(){ //constructor } public void foo(){ …
fo_x86
  • 2,583
  • 1
  • 30
  • 41