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
386
votes
16 answers

Why are only final variables accessible in anonymous class?

a can only be final here. Why? How can I reassign a in onClick() method without keeping it as private member? private void f(Button b, final int a){ b.addClickHandler(new ClickHandler() { @Override public void…
user467871
320
votes
18 answers

How are Anonymous inner classes used in Java?

What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of Java?
Warrior
  • 39,156
  • 44
  • 139
  • 214
154
votes
12 answers

How to pass parameters to anonymous class?

Is it possible to pass parameters, or access external parameters to an anonymous class? For example: int myVariable = 1; myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // How would one…
Lewis
  • 3,375
  • 3
  • 29
  • 26
148
votes
2 answers

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() { …
Bob
  • 5,510
  • 9
  • 48
  • 80
123
votes
9 answers

Java8 Lambdas vs Anonymous classes

Since Java8 has been recently released and its brand new lambda expressions looks to be really cool, I was wondering if this means the demise of the Anonymous classes that we were so used to. I've been researching a bit about this and found some…
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
102
votes
6 answers

Multiple inheritance for an anonymous class

How can an anonymous class implement two (or more) interfaces? Alternatively, how can it both extend a class and implement an interface? For example, I want to create an object of anonymous class that extends two interfaces: // Java 10 "var" is…
user707549
94
votes
6 answers

Java 8 Lambda Expressions - what about multiple methods in nested class

I'm reading about the new features at: http://www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html I saw the example below: Using Anonymous Class: button.addActionListener(new ActionListener() { public void…
Menelaos
  • 23,508
  • 18
  • 90
  • 155
85
votes
7 answers

Can we create an instance of an interface in Java?

Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { …
Ninja
  • 1,166
  • 2
  • 9
  • 16
72
votes
6 answers

What is the $1 in class file names?

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 …
omg
  • 136,412
  • 142
  • 288
  • 348
65
votes
9 answers

How to start anonymous thread class

I have the following code snippet: public class A { public static void main(String[] arg) { new Thread() { public void run() { System.out.println("blah"); } }; } } Here, how do I call…
noMAD
  • 7,744
  • 19
  • 56
  • 94
65
votes
5 answers

How do I replace an anonymous class with a lambda in Java?

I've got this code but IntelliJ tells me to replace anonymous with lambda but I don't know how. can anyone help me with this? Here is my code: soundVolume.valueProperty().addListener(new ChangeListener() { public void…
yukashima huksay
  • 5,834
  • 7
  • 45
  • 78
64
votes
6 answers

Cast to Anonymous Type

I had the following problem today, and I was wondering if there is a solution for my problem. My idea was to build anonymous classes and use it as a datasource for a WinForm BindingSource: public void Init() { var option1 = new …
gsharp
  • 27,557
  • 22
  • 88
  • 134
63
votes
4 answers

Is it possible to make an anonymous class inherit another class?

This is a long shot, but I have a funny coding situation where I want the ability to create anonymous classes on the fly, yet be able to pass them as a parameter to a method that is expecting an interface or subclass. In other words, I'd like to be…
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
61
votes
5 answers

Is there a way to convert a dynamic or anonymous object to a strongly typed, declared object?

If I have a dynamic object, or anonymous object for that matter, whose structure exactly matches that of a strongly typed object, is there a .NET method to build a typed object from the dynamic object? I know I can use a LINQ…
ProfK
  • 49,207
  • 121
  • 399
  • 775
57
votes
9 answers

Setting outer variable from anonymous inner class

Is there any way to access caller-scoped variables from an anonymous inner class in Java? Here's the sample code to understand what I need: public Long getNumber(final String type, final String refNumber, final Long year) throws ServiceException { …
TC1
  • 1
  • 3
  • 20
  • 31
1
2 3
46 47