anonymous-class

Basic Java Multi-Threading Question

When an object is instantiated in Java, is it bound to the thread that instantiated in? Because when I anonymously implement an interface in one thread, and pass it to another thread to be run, all of its methods are run in the original thread. If they are bound to their creation thread, is there anyway to create an object that will run ...

What is this technique called in Java?

I'm a C++ programmer, and I was reading this site when I came across the example below. What is this technique called in Java? How is it useful? class Application { ... public void run() { View v = createView(); v.display(); ... protected View createView() { return new View(); } ... } class ApplicationTest extends...

access exception when invoking method of an anonymous class using java reflection

Hello I'm trying to use an event dispatcher to allow a model to notify subscribed listeners when it changes. the event dispatcher receives a handler class and a method name to call during dispatch. the presenter subscribes to the model changes and provide a Handler implementation to be called on changes. Here's the code (I'm sorry it's ...

What does that Java construct do?

Hi All I new to java so bear with me if this is a ridiculously simple question but I am curious about this method call which has {code} being taken in - see code below for an example in the method addSelectionListener. What is the purpose of this? I have been looking through docs for an explaination but cant seem to find what this pra...

Returning an anonymous class that uses a final primitive. How does it work?

Hi, I was wondering if someone could explain how the following code works: public interface Result { public int getCount(); public List<Thing> getThings(); } class SomeClass { ... public Result getThingResult() { final List<Thing> things = .. populated from something. final int count = 5; return new Result { ...

Why is an anonymous inner class containing nothing generated from this code?

When run through javac on the cmd line Sun JVM 1.6.0_20, this code produces 6 .class files OuterClass.class OuterClass$1.class OuterClass$InnerClass.class OuterClass$InnerClass2.class OuterClass$InnerClass$InnerInnerClass.class OuterClass$PrivateInnerClass.class When run through JDT in eclipse, it produces only 5 classes. OuterC...

Can a method call with an anonymous inner class as an argument be verified with mockito?

I suspect this isn't possible as the anonymous inner class is private. Can I verify that the method was called without worrying about the argument? I'm tying to test this: http://bsnyderblog.blogspot.com/2010/02/using-spring-jmstemplate-to-send-jms.html With something like: verify(jmsTemplate, times(1)).send(); But send() needs an a...

Can anybody explain the working of following code...?

Can anybody explain the working of following code...? interface myInterface{} public class Main { public static void main(String[] args) { System.out.println(new myInterface(){public String toString(){return "myInterfacetoString";}}); System.out.println(new myInterface(){public String myFunction(){return "myIn...

How can I load class's part using linq to sql without anonymous class or additional class?

class Test { int Id{get;set;} string Name {get;set;} string Description {get;set;} } //1)ok context.Tests.Select(t => new {t.Id, t.Name}).ToList().Select(t => new Test{Id = t.Id, Name = t.Name}); //2)ok class TestPart{ int Id{get;set;} string Name {get;set;} } context.Tests.Select(t => new TestPart{Id = t.Id, Name = t.Name...

Java anonymous class efficiency implications

Is there any difference in efficiency (e.g. execution time, code size, etc.) between these two ways of doing things? Below are contrived examples that create objects and do nothing, but my actual scenarios may be creating new Threads, Listeners, etc. Assume the following pieces of code happen in a loop so that it might make a difference...

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 requires a list of elements with each element implementing an interface (ISelectable). I kno...

adhoc struct/class in C#?

Currently i am using reflection with sql. I find if i want to make a specialize query it is easiest to get the results by creating a new class inheriting from another and adding the 2 members/columns for my specialized query. Then due to reflections in the lib in my c# code i can write foreach(var v in list) { v.AnyMember and v.MyExtraMe...

Odd linked list/anonymous class behavior - executing when added?

This question is related to How to Queue and Call Actual Methods... Anyway, I've decided to (after all) go with the anonymous class idea. The problem is that when I ADD my anonymous class to the linked list, it's actually calling execute() immediately... and it shouldn't be. Execute() is to be called later. Anyway, this is what I have: ...

Referencing variables from anonymous classes in Java

I am writing a setonclick listner, and I want to be able to refer to that button so that I can change its properties. I.e. make it disabled? I get thismessage: Cannot refer to a non-final variable confirmButton inside an inner class defined in a different method confirmButton.setOnClickListener(new View.OnClickListener() { ...

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() { Log.e(TAG, "Called delegate!!!! "+ userID); } }); } I get this error: Cannot ...

Anonymous classes, temporary data, and collections of anonymous classes

I'm new to anonymous classes, and today I think I ran into the first case where I felt like I could really use them. I'm writing a method that would benefit from storing temporary data inside of a class, and since that class doesn't have any meaning outside of that method, using an anonymous class sure made sense to me (at least at the ...

InstantiationException on newInstance of generated anonymous class

Update: this is more-or-less a dupe, and it turns out to be compiler magic adding a constructor to pass in the local variable in build2. Given an interface like such: public interface IFoo { public int get(); } The code below prints 1, 1, 2 and then throws an exception when trying to call getClass().newInstance() on the value ...

Access anonymous inner class variables

How to access i from the outer class? HashSet<Integer> hs=new HashSet<Integer>(){ int i=30; }; I can do it like this int k=new HashSet<Integer>(){ int i=30; }.i; But if I get 'i' then I cannot get the instance of hashset.Is there a way to get both?The question just out of curiosity.It doesn't have much practical ...

ArrayList.remove(int index) not working with non-anonymous class object

Hi All, ArrayList.remove(int index) is working with the anonymous instance of ActionListener class :- DeleteModule.java :- import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionP...

Java/Android: anonymous local classes vs named classes

I would like to ask what is the good practice on using anonymous classes vs. named inner classes? I am writing an Android application, which includes many UI elements (buttons, text fields, etc). For many of them I need some kind of listeners, so in onCreate of the application I have bunch of quite small anonymous classes like: someBu...