anonymous-class

Why can't I call a method outside of an anonymous class of the same name

The code at the end produces a compile error: NotApplicable.java:7: run() in cannot be applied to (int) run(42); ^ 1 error The question is why? Why does javac think I am calling run(), and does not find run(int bar)? It correctly called foo(int bar). Why do I have to use NotApplicable.this.run(42);?...

Should javac find methods outside of an anonymous class of the same name?

This question is a follow up to: Why can’t I call a method outside of an anonymous class of the same name This previous question answer why, but now I want to know if javac should find run(int bar)? (See previous question to see why run(42) fails) If it shouldn't, is it due to a spec? Does it produce ambiguous code? My point is, I t...

Accessing inner anonymous class members

Is there any way other than using reflection to access the members of a anonymous inner class? ...

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 = Display.getDefault(); display.syncExec(new Runnable() { public void run() { ...

Does Python have something like anonymous inner classes of Java?

In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class. Suppose that you want create a subclass of OptionParser that overrides only a single method (for example exit()). In Java you can write something like this: new OptionParser () { publi...

Is usage of anonymous classes in Java considered bad style or good?

I know anonymous classes save typing, if it comes to implementing Listener and similar stuff. They try to be a replacement for some usages of closures. But what does the community think about the value of these language-feature? Does it make sense and you use it regularly? Makes the saved typing the code clearer, more understandable and...

C#: Creating an instance of an abstract class without defining new class

I know it can be done in Java, as I have used this technique quite extensively in the past. An example in Java would be shown below. (Additional question. What is this technique called? It's hard to find an example of this without a name.) public abstract class Example { public abstract void doStuff(); } public class StartHere{ p...

C# Equivalent of Java anonymous inner classes with init blocks

In Java, i like to use constructs such as List<String> list = new ArrayList<String>() {{add("foo");}}; Is there a way to do this in 1 line in C#, too? ...

Why group by key of anonymous objects does not behave the way expected ?

I have a csv file of this formart A,B,value a1,b1,10 a2,b1,12 a2,b1,15 a2,b2,14 a1,b1,12 which I am converting as datatable in my application. Dim enumerable = _dt.AsEnumerable Dim groupedResults = enumerable.GroupBy( _ Function(x) _ New With { _ ...

Unintended consequences of anonymous class created just for sake of adding instance initialization block

This is a question about Java code such as: List<String> list = new ArrayList<String>() {{add("hello"); add("goodbye");}} where the programmer has extended ArrayList anonymously just for the sake of shoving in an instance initialization block. The question is: if the sole intention of the programmer is merely to achieve the same as: ...

Anonymous vs named inner classes? - best practices?

I have a class, let's call it LineGraph, that renders a line graph. I need to subclass it, but the derived class is only used in one place and is coupled to the class that uses it. So I am using an inner class. I see two ways to do this: Anonymous inner class public class Gui { LineGraph graph = new LineGraph() { // extra ...

Anonymous delegate implementation in Objective-C?

Is it possible to declare anonymous implementations of things like Delegates in Objective-C. I think I have the terminology right, but here's a java example: myClass.addListener(new FancyInterfaceListener({ void onListenerInterestingAction(Action a){ ....interesting stuff here } }); So for example to handle an UIAction...

Matlab / Java API callback

I'm designing an API (in Java) and expect to have users accessing the API from Matlab. The problem is that I want the API to provide a piece of functionality like: javaApi.waitUntilPredicateIsTrue(Predicate<JavaObj> test); My API (in the background) gets hold of instances of Java Obj (via some mechanism, e.g. polling). I want this API...

Use of anonymous class in C#

We all know that, when we create anonymous class var Employee = new { ID = 5, Name= "Prashant" }; at run time it will be of type <>f__AnonymousType0<int,string> is there any way to specify some meaningfully name to these classes ?? ...

Will dispose be called for anonymous variables?

For example, int myResult= (new UnmanagedResourceUsingMemorySuckingPig()).GetThingsDone(id); There is no using block, no obvious way to use a using block, no obvious way to call Dispose(). And of course UnmanagedResourceUsingMemorySuckingPig does implement IDisposable. ...

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 decremented each time it was clicked. The JButton and its ActionListener both had to be anonym...

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; connection.call("getUserid", new Responder() { public void onResult(final int result) { System.out.println(...

what are the $1 in class file?

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir Volume in drive C has no label. Volume Serial Number is 2041-64E7 Directory of C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet 2009-07-02 23:54 . 2009-07-02 23:54 .. 2004-09-06 14:57 582 WelcomeApplet.ht...

Access "this" from Java anonymous class

Given the following code: public interface Selectable { public void select(); } public class Container implements Selectable { public void select() { ... } public void createAnonymousClass() { Selectable s = new Selectable() { public void select() { //see comment below. } }; } } I want to acces...

Uninstantiated Anonymous Classes in Java

It's been about 6 years since I've written Java, so please excuse the rust. I'm working with a library method that requires that I pass it Class objects. Since I'll have to invoke this method a dynamic number of times, each time with a slightly different Class argument, I wanted to pass it an anonymous class. However, all the document...