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

Automatic replacing all anonymous inner class to lambda in Intellij Idea

I'm working on a project which is implemented in java 7 and now I want to migrate to java 8. I use IntelliJ Idea and want to find all "new inner class occurrence" whit regex finding("new .(.)\s*{[.|\s]*") to replace with their anonymous versions,…
mahdi_12167
  • 491
  • 4
  • 9
29
votes
6 answers

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,…
Phil
  • 5,595
  • 5
  • 35
  • 55
26
votes
5 answers

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…
rustyshelf
  • 44,963
  • 37
  • 98
  • 104
25
votes
5 answers

Why an Anonymous class can't implement multiple interfaces directly? Simply because of syntax or there is another reason?

In there an internal issue why java anonymous classes cannot implement and subclass at the same time? Or is it just because the syntax?
Zsombi
  • 251
  • 3
  • 3
25
votes
5 answers

Is cyclic dependency between anonymous class and parent class wrong?

I have following snippet of code: public class Example { private Integer threshold; private Map history; protected void activate(ComponentContext ctx) { this.history = Collections.synchronizedMap(new LinkedHashMap
24
votes
3 answers

Interface-implementing anonymous class in C#?

Is there a construct in C# which allows you to create a anonymous class implementing an interface, just like in Java?
RandyTek
  • 4,374
  • 3
  • 23
  • 32
24
votes
3 answers

Access method of outer anonymous class from inner anonymous class

I instantiate an anonymous class with a method that instantiates another anonymous class, and from this inner anonymous class I want to call a method belonging to the outer anonymous class. To illustrate it, suppose I have this interface: interface…
Rulle
  • 4,496
  • 1
  • 15
  • 21
24
votes
9 answers

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…
vipirtti
  • 1,058
  • 4
  • 15
  • 24
23
votes
5 answers

What is the an effective design pattern/style for designing a rule engine in Java?

I am implementing a rule-engine in Java. My rule-engine predefines a list of independent rules and rule sets. A rule here is simply a piece of logic. And a rule set combines these simple rules into an ordered set. I am a decent java developer but…
Shankar
  • 3,496
  • 6
  • 25
  • 40
23
votes
2 answers

Empty anonymous inner class in java

I was looking into the Guava library and I came across an empty anonymous inner class in TypeToken. TypeToken> stringListTok = new TypeToken>() {}; What is exactly use of empty anonymous inner class and what are the useful…
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
22
votes
5 answers

Stop a periodic task from within the task itself running in a ScheduledExecutorService

Is there a nice way to stop the repetition of task from within the task itself when running in a ScheduledExecutorService? Lets say, I have the following task: Future f = scheduledExecutor.scheduleAtFixedRate(new Runnable() { int count = 0; …
22
votes
1 answer

Is a good practice create anonymous AsyncTask for parallel small known freeze process?

E.g.: you gonna do something that will take a few seconds and don't wanna freeze your UI thred, right? You could use an AsyncTask but you don't wanna create a external (or inner) class to solve a small freeze problem. So, is a good pratice do…
Idemax
  • 2,712
  • 6
  • 33
  • 66
22
votes
6 answers

Calling newly defined method from anonymous class

I instantiated an object of an anonymous class to which I added a new method. Date date = new Date() { public void someMethod() {} } I am wondering if it is possible to call this method from outside somehow similar to: date.someMethod();
user1544745
  • 677
  • 2
  • 8
  • 15
21
votes
3 answers

Anonymous class replaced with lambda expressions

I have sample code which uses Java 8 new stream functionality (get a range of int values 1 .. 20, skip the first 9, then take remaining 10, each int value: reduce by one and multiply by 2). System.out.println(Arrays.toString( …
Tom
  • 26,212
  • 21
  • 100
  • 111
20
votes
2 answers

Passing final variables to anonymous classes

In final variable passed to anonymous class via constructor, Jon Skeet mentioned that variables are passed to the anonymous class instance via an auto-generated constructor. Why would I not be able to see the constructor using reflection in that…
Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26
1 2
3
46 47