Questions tagged [anonymous-inner-class]

Anonymous Inner Classes are local classes that are declared and instantiated inline.

These are local classes that are declared and instantiated inline, often in the middle of an expression or as an argument of a method. They can only directly extend one class or implement one interface. They can specify arguments to the constructor of the superclass, but cannot otherwise have a constructor (however, this is not a limitation, since it can have an instance initializer block to perform any initialization).

246 questions
4
votes
1 answer

Netbeans fails to find main class when creating anonymous subclass of inner class of anonymous subclass

When I attempt to create a new anonymous Action subclass inside the initialization of an anonymous subclass of its containing class's containing class, Netbeans suddenly fails to find the main class when running, despite being able to clean+build…
4
votes
3 answers

Access an instance of a class from anonymous class argument

I can't seem to find an answer to this through all the anonymous inner class questions on the site. public void start() { /* Ask the user to login */ final LoginFrame login; login = new LoginFrame(new ActionListener() { …
Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
4
votes
3 answers

Build an anonymous inner class and call its methods

I searched for this but unfortunately failed to find matches, I have this local anonymous inner class inside a method like this:- new Object(){ public void open(){ // do some stuff } public void dis(){ // do some stuff …
Scorpion
  • 577
  • 4
  • 21
4
votes
2 answers

Instantiating anonymous inner classes in Java with additional interface implementation

Let's say I have the following two class/interface definitions: public abstract class FooClass { public abstract void doFoo(); } and public interface BarInterface { public void doBar(); } If I want to make an anonymous inner class that…
Markus A.
  • 12,349
  • 8
  • 52
  • 116
4
votes
2 answers

Accessing outer inner class from nester inner class

I have the following code: public class Bar {} public class FooBar {} public class Foo { public void method() { new Bar() { void otherMethod() { } void barMethod() { new FooBar() { …
WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
4
votes
1 answer

JDK Hashmap Source Code - Anonymous Inner Classes and Abstract Instantiation?

Problem I'm trying to understand how Sun implemented the entrySet, keySet and values methods of the HashMap class but I'm coming across code that doesn't make sense to me. I understand conceptually that these methods return views directly linked to…
user2858650
4
votes
5 answers

Java ActionListeners

I am going to be developing a game in Java, and it will have many listeners( action, key, mouse, etc..). My Question is what is the preferable way to implement the listeners. Method 1: this.addActionListener(new ActionListener() { // Overide…
user2444217
  • 581
  • 2
  • 7
  • 16
4
votes
2 answers

Pass outer anon class ref to a method in an inner anon class

How to pass outer anon class ref to a method in an inner anon class in Java? I have a method that makes async call to a server - sendCall(some_args, callback). The callback is represented by anonymous class (let's name it OuterAnon) and contains a…
Dragon
  • 2,431
  • 11
  • 42
  • 68
3
votes
1 answer

Why do I "need opCmp" for an anonymous class?

I'm not exactly sure how to explain this, so please ask me to clarify anything that doesn't make sense. I have an interface and a template function which returns functions which return anonymous inner classes based on the compile time…
Tim
  • 1,011
  • 6
  • 12
3
votes
3 answers

Why require local variables to be final when accessing from anonymous inner classes?

We all know you can't do things like this: int a = 7; new Runnable() { public void run() { System.out.println(a); } }.run(); ... ...without making a final. I get the technical reason why and it's because local variables live on…
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
3
votes
7 answers

Anonymous Inner Classes: When are they (in)appropriate?

Take the following example. There's an object I want to use, call it a Doodad. Doodad elements have poorly implemented handling of browser events. Typical instantiation of a Doodad would be Doodad someDoodad = new Doodad();. Obviously this isn't…
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
3
votes
4 answers

What is this in java? Attaching methods "on the fly"?

I saw something like this today: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); What does the following part mean? new AClass(){ this part } Can I…
lucaswxp
  • 2,031
  • 5
  • 23
  • 34
3
votes
4 answers

Disabling a button inside an anonymous inner class

I have these line of code and I want to disable the button after a passenger has been added. I want to disable the button. seats[i].setEnabled(false) won't work since it's inside an anonymous inner class. JButton [] seats = new JButton [40];…
dave
  • 419
  • 1
  • 5
  • 10
3
votes
3 answers

How to call multiple methods of Anonymous class?

In below chunk of code, I am creating an anonymous class by extending LinkedList but don't know how can I call-up multiple methods outside the anonymous class. I am able to call one method though as mentioned in end by .dummy1() void methodOC_3()…
3
votes
2 answers

best practices for accessing a for loop variable inside an inner class

While making an android app I encountered and issue regarding accessing a non final variable from an inner class. used This as a reference. I wanted to ask what is the "proper" and efficient way to do this? my two solutions are below: for (Button…
Ali Pardhan
  • 194
  • 1
  • 14