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
19
votes
1 answer

php anonymous class extends dynamic

anyone of you know how to get effect like in these code public function create(SomeInterface $obj) { $class = get_class($obj); return new class extends $class { //some magic here } } Obvious that code will not work in…
adeptofvoltron
  • 369
  • 1
  • 9
18
votes
3 answers

Kotlin object expressions: Comparator example

This code basically sorts the array in a descending order: val arrayList = arrayListOf(1, 5, 2) Collections.sort(arrayList, object : Comparator { override fun compare(x : Int, y: Int) = y - x }) How in the world does overriding the compare…
Rafael Saraiva
  • 908
  • 2
  • 11
  • 23
18
votes
4 answers

Access outer anonymous class from inner anonymous class

I'm just curious. Is there a way to access parent in anonymous class that is inside another anonymous class? I make this example create a JTable subclass (anonymous class) override changeSelection and inside i create another anonymous class.…
nachokk
  • 14,363
  • 4
  • 24
  • 53
18
votes
3 answers

What is this constructor call with following double braces?

Unfortunately I haven't coded Java for about five years and I absolutely can not remember how or why the following code is working. I stumbled across a similar example and broke it down to this. The emphasis is on the part below the comment: I…
klekker
  • 185
  • 1
  • 7
17
votes
4 answers

Are all final variables captured by anonymous classes?

I thought I knew the answer to this, but I can't find any confirmation after an hour or so of searching. In this code: public class Outer { // other code private void method1() { final SomeObject obj1 = new SomeObject(...); …
ajb
  • 31,309
  • 3
  • 58
  • 84
17
votes
2 answers

What does a variable being "effectively final" mean?

The documentation on Anonymous Classes states An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. I don't understand what does a variable being "effective final" mean. Can…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
16
votes
2 answers

Dynamic construction of anonymous class confusion

I'm trying to make instances of anonymous classes using reflection. But ocassionally I've seen strange behaviour during instantination. Please, look at these similar fragments of code public class HideAndSeek { @SuppressWarnings("unchecked") …
stemm
  • 5,960
  • 2
  • 34
  • 64
16
votes
1 answer

Is it legal to define an anonymous struct?

Is the following code legal?: struct { int x; }; This code simply defines an unnamed structure. I do not intend to create objects of this type, nor do I need this structure in any other way. It simply appears in the source as a side effect of…
Igor G
  • 1,838
  • 6
  • 18
16
votes
3 answers

LINQ select query with Anonymous type and user Defined type

Anonymous class has read only properties in c#. Which is often used to to declare in linq select query to get particular values from database. In my code I have the following query.The thing that confused me selecting new object of anonymous class…
Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63
16
votes
4 answers

Why can an anonymous class access non-final class member of the enclosing class

We know that only final local variables can be accessed in an anonymous class, and there is a good reason here: Why are only final variables accessible in anonymous class?. However, I found that an anonymous class can still access a non-final…
ctk2021
  • 361
  • 2
  • 12
16
votes
3 answers

NotSerializableException on anonymous class

I have an interface for filtering items: public interface KeyValFilter extends Serializable { public static final long serialVersionUID = 7069537470113689475L; public boolean acceptKey(String iKey, Iterable iValues); public…
Shaharg
  • 971
  • 1
  • 11
  • 26
15
votes
3 answers

When an anonymous class with no references to its enclosing class is returned from an instance method, it has a reference to this. Why?

When an anonymous class with no references to its enclosing class is returned from an instance method, it has a reference to this. Why? Consider the following code: package so; import java.lang.reflect.Field; public class SOExample { private…
Robert Bain
  • 9,113
  • 8
  • 44
  • 63
14
votes
6 answers

Can anonymous class be used as return types in C++?

Is there any way to use anonymous classes in C++ as return types? I googled that this may work: struct Test {} * fun() { } But this piece of code doesn't compile, the error message is: new types may not be defined in a return type Actually the…
cheng
  • 2,106
  • 6
  • 28
  • 36
14
votes
8 answers

Why does adding a public field to an anonymous class in Java not work?

I have an example class defined like below: public class FooBar { void method1(Foo foo){ // Should be overwritten ... } } Later, when I try this: FooBar fooBar = new FooBar(){ public String name = null; @Override void method1(Foo…
Jessie A. Morris
  • 2,267
  • 21
  • 23
14
votes
4 answers

Anonymous struct in typedef of trait class

Sorry for the funny title. Prior to C++0x, there are restrictions in the use of function-local structs (“local types”) as template arguments. My question is essentially if similar restrictions apply to anonymous structs. Specifically, in the context…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214