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
0
votes
2 answers

Getting inner class type name

I'm trying to get the name of a type (which is an interface) that is instantiated within a class but the available methods I've tried do not return the actual name of the type. Example: To get the name I would do: class Test { void test(Object…
Rustom
  • 11
  • 5
0
votes
3 answers

Need explanation on a method-call

In the code below: public File[] findFiles (String path) { FilenameFilter textFilter = new FilenameFilter() { @override public boolean accept(File dir, String name) { …
Melika Barzegaran
  • 429
  • 2
  • 9
  • 25
0
votes
3 answers

Why use Anonymous Inner classes, and what are the alternatives?

I've recently got into Android and have been looking at examples about Inner classes but don't really understand what the use of them is. They are used often when making listeners and when making a full class is unnecessary right? Maybe someone can…
0
votes
1 answer

Anonymous Inner Class in Java not working. Why?

The class in question is this. As you can see, it's very simple. Just to learn about the workings of anonymous inner classes. In this case I am getting 4 errors saying that the symbols WaterLevel and ChlorineLevel cannot be found. Can you see what…
Maria
  • 565
  • 6
  • 20
0
votes
1 answer

anonymous-inner-classes vs static field

I prefer to use static field for instances of classes that not store his state in fields instead anonymous-inner-classes. I think this good practice for to less memory and GC usage if method sort(or other) call very often. But my colleague prefer to…
Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
0
votes
2 answers

Dynamic Android Table

I ran into another problem. Found a tutorial on how to create a dynamic table,followed it but mine doesnt seems to work,when adding the dynamic rows. The static column headings works fine. public class Leaders extends Activity { TableLayout tl; …
user1758720
0
votes
2 answers

Instantiate Java anonymous inner class with generic using Class in variable

Is it possible in Java 7 to instantiate an anonymous inner class with a generic type using a Class object that I have in hand? Here's the simplified version of the generic class I'm trying to instantiate: abstract class DomainBuilder { …
Ted Naleid
  • 26,511
  • 10
  • 70
  • 81
0
votes
1 answer

Why should we change the modifiers of fields outside an inner class to final?

I have a question about why should we set a field final when we use it in an innerclass? for example why should we set the modifier of textField to final? My question is that why it will not be available if we do not declare it as final? final…
Siavash
  • 503
  • 1
  • 5
  • 25
0
votes
2 answers

In Java how can I access a variable from outside the anonymous class

for example: for (int i = 0; i < 10; i++){ SomeClass something = new SomeClass(); something.setOnClickListener(new OnClickListener() { public void onClick(){ doSomething(i); } }); } I am not allowed to use the variable…
nomnom
  • 1,560
  • 5
  • 17
  • 31
0
votes
2 answers

Java Eclipse android syntax errors with anonymous inner class

I am trying to program a sort of 'menu' in android with 3 buttons, and OnClickListeners recording input from each. However, I am getting some strange syntax errors. Here is my MainActivity.java: package com.example.galaxydefense; import…
lkjhgfdsa
  • 47
  • 2
  • 8
0
votes
3 answers

Why am I getting this error as I try from withing the anonymous class?

I get an error that says : no suitable method found for showMessageDialog(,String,String,int) as I try to use the JOptionPane.show... method. Why is that ? private void connectivityChecker() { Runnable r = new Runnable() { …
saplingPro
  • 20,769
  • 53
  • 137
  • 195
0
votes
1 answer

saving data generated inside anonymous inner classes

I have a JTextField in my program which I hooked up a Keyboard Listener to through the use of an anonymous inner class. the listener clears the text box and saves the word currently on it. I want to be able to use that word I got out of it in other…
gadu
  • 1,816
  • 1
  • 17
  • 31
0
votes
2 answers

Variables accessible to anonymous inner class in Java

I want to use an anonymous inner class to handle mouse actions for an object in my program. However, depending on a parameter, I want the inner class to act differently. For example: Rectangle r1 = rectangleBuilder (Color.Red); Rectangle r2 =…
James Andrews
  • 3,257
  • 3
  • 31
  • 45
0
votes
1 answer

External call of SwingWorker custom methods

... SwingWorker worker = new SwingWorker(){ String a = "a"; getA() { return a; } protected boolean doInBackground() throws Exception{ return true; …
Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
0
votes
5 answers

Anonymous inner classes: declared as instance variables vs creating on the fly

I am refactoring a class with a public facing interface and thinking about the usage led me to ask: What is the difference between declaring the following within some larger class (as an instance variable): private final OnClickListener…
Daniel Smith
  • 8,561
  • 3
  • 35
  • 58