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

Java anonymous classes and Garbage Collector

Lets assume that some class is not reachable, but there are another anonymous classes generated by the class which are reachable. Could the first one be removed by the Garbage Collector? Example: class Outer { public Object getInner() { …
Ilya Lakhin
  • 1,904
  • 1
  • 16
  • 31
7
votes
2 answers

Anonymous extending a class and implementing an interface at the same time?

Suppose I have an interface called Interface, and a concrete class called Base, which, to make thing a bit more complicated, has a ctor that requires some arguments. I'd like to create an anonymous class that would extend Base and implement…
user113454
  • 2,273
  • 9
  • 28
  • 35
7
votes
4 answers

Is it possible to give a definition of a class in C++ during allocation, as is allowed in java

Or simply put can I do some thing like class A { public: virtual void foo() = 0; }; class B { public: A *a; b(){ a = new A() { void foo() {printf("hello");} } };
user210504
  • 1,749
  • 2
  • 17
  • 36
7
votes
1 answer

Lambda expression and equivalent anonymous class

If I have a method that takes a single-method interface as an argument, I can call it like this: foo(new Bar() { @Override public String baz(String qux) { return modify(qux) + transmogrify(qux); } } But if I have to call foo…
k314159
  • 5,051
  • 10
  • 32
7
votes
2 answers

Why do anonymous classes capture "this" even if they don't need to?

Given this code: class Foo {} public class Test { public Foo makeFoo(String p, String q) { return new Foo(){ public void doSomething() { System.out.println(p); …
7
votes
3 answers

What's the best way to get a return value out of an asyncExec in Eclipse?

I am writing Eclipse plugins, and frequently have a situation where a running Job needs to pause for a short while, run something asynchronously on the UI thread, and resume. So my code usually looks something like: Display display =…
Uri
  • 88,451
  • 51
  • 221
  • 321
7
votes
3 answers

anonymous class as generic parameter

I want to create a class that gets an object from anonymous class definition to store. I used a generic typed class to achieve that. Then i want to define some operations using functional interfaces that gets this object as a parameter to work…
ArcticLord
  • 3,999
  • 3
  • 27
  • 47
7
votes
2 answers

Why should I use anonymous classes in Android instead of class redefinition?

I'm new in Android dev. I read some books about it. And all authors strongly recommend to use anonymous classes instead of class redefinition. They say that TextView txtTitle; ... txtTitle.setOnClickListener(new OnClickListener() { @Override …
7
votes
1 answer

How to anonymous classes in TypeScript

I am trying to create a helper function, to add currying, to generate common setups for classes. For example: class Person { private name: string; private sex: string; constructor (name: string, sex: string) { this.name = name; …
JL235
  • 2,455
  • 2
  • 19
  • 14
7
votes
3 answers

Programming convention on Anonymous Class vs Implementing Interface

From the android development perspective, while you are programming which way do you prefer to implement for listener? Or which way do you think is the best for readable code? I gave two example about these things but think more complex classes such…
6
votes
2 answers

Need help with callbacks and anonymous classes in Java

I am using some third party library to connect to a server via async protocol and get response back. For example method to get userid by username looks like this: public int getUserid(String username) { int userid = 0; …
serg
  • 109,619
  • 77
  • 317
  • 330
6
votes
1 answer

Why are all anonymous classes implicitly final?

According to the JLS: 15.9.5 Anonymous Class Declarations An anonymous class declaration is automatically derived from a class instance creation expression by the compiler. An anonymous class is never abstract (§8.1.1.1). An anonymous class is …
corsiKa
  • 81,495
  • 25
  • 153
  • 204
6
votes
3 answers

Scala 3 enum method override

Is there a way to override method in Scala 3 enum just like in Java? public enum Test { ONE { @Override public int calc() { return 1; } }, TWO { @Override public int calc() { …
Andrei Yusupau
  • 587
  • 1
  • 11
  • 30
6
votes
2 answers

Reference of enclosing object escape through anonymous class- java

I am reading Java concurrency in practice and the below examples are from that. And my questions are What do they mean by this reference escape?. What will be the problem? . How does the this reference escapes from doSomething(e). public class…
John
  • 2,682
  • 5
  • 23
  • 24
6
votes
3 answers

How to extend an object of the anonymous class

I have class method: public object MyMethod(object obj) { // I want to add some new properties example "AddedProperty = true" // What must be here? // ... return extendedObject; } And: var extendedObject = this.MyMethod( new { …
Jean Louis
  • 1,485
  • 8
  • 18
  • 33