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
3
votes
3 answers

How does the interface in anonymous inner class work?

interface MyInter { public void display(); } class OuterClass8 { public static void main(String arg[]) { MyInter mi=new MyInter() { public void display() { System.out.println("this is anonymous…
Ajinkya
  • 147
  • 1
  • 2
  • 11
3
votes
4 answers

java, reflection , innerclass,

Hi i want to get the object of inner class using reflection but i am getting some error in it. code is:- package reflaction; public class MyReflection { public static void main(String[] args) throws ClassNotFoundException, InstantiationException,…
user2142786
  • 1,484
  • 9
  • 41
  • 74
3
votes
4 answers

Anonymous inner class must extend some superclass?

I am learning java swing and came across the concept of anonymous inner class. After studying a bit at this link, I feel that an anonymous inner class must always extend some other class since the method in which it gets passed as a parameter is…
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
3
votes
1 answer

Hiding JFrame with anonymous ActionListener in a JButton

I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. Each of these should launch a new window and hide the welcome window. I know I can do it with setVisible(false);. But I can't make it work yet. This is…
2
votes
3 answers

Break out of a method from anonymous inner class

I have a method: void someMethod(String someString) final String[] testAgainst = {...}; .... for(int i = 0; i < testAgainst.length; i++) { if (someString.equals(testAgainst[i])) { AlertDialog.Builder builder = new…
gobernador
  • 5,659
  • 3
  • 32
  • 51
2
votes
2 answers

Modify an object from OnClickListener non-final variable permited

I have an adapter where I load my users and one button/textfiled for send invitations. In the adapter I do this: @Override public View getView(int position, View convertView, ViewGroup parent) { UserAgenda ua =…
Dayerman
  • 3,973
  • 6
  • 38
  • 54
2
votes
2 answers

How can I access private class members of container class within the anonymouse inner class?

How can I access all the member field of the class which contains the function initTimer() from within the AbstractActionClass? Thanks private void initTimer() { Action updateClockAction = new AbstractAction() { public…
JavaSa
  • 5,813
  • 16
  • 71
  • 121
2
votes
0 answers

Accessing "this" of (not 'from') enclosing anonymous inner class

I have a situation where I need to grab a reference to an enclosing anonymous inner class: I have some asynchronous method doSomething(...) that calls a result-callback when it's done (given to me by a library; can't change this) and I'm trying to…
Markus A.
  • 12,349
  • 8
  • 52
  • 116
2
votes
3 answers

Work-around for enclosing instance ineligible for garbage collection

The enclosing instance here isn't eligible for garbage collection as long as the enclosed instance is alive, right? interface Foo { T compute(); default Foo memoize() { return new Foo<>() { T result = null; …
gdejohn
  • 7,451
  • 1
  • 33
  • 49
2
votes
4 answers

I upgraded to Android Studio 2.3.3 and an old, bug-free program now gives error: "Fragments should be static..."

The full text of the error is: C:\Users\Dov\Google Drive\AndroidStudioProjects\FlagQuiz - Copy (2)\app\src\main\java\com\dslomer64\flagquiz\QuizFragment.java Error: Fragments should be static such that they can be re-instantiated by the system, and…
2
votes
3 answers

Initialize variable with complex expression

Is there a way in Java 8 to simultaneously declare and initialize a final variable with the result of a complex expression? In other words, is something like the following possible? final int x = [capturedVariable] { switch (capturedVariable) { …
François Beaune
  • 4,270
  • 7
  • 41
  • 65
2
votes
4 answers

class instance with override inside method parameter

can any one please help me understand this code block of java String [] files= file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return…
2
votes
1 answer

Error implementing interface as anonymous inner class

I'm learning JavaFX using Intellij IDEA. When compiling the following code: public class Main extends Application implements EventHandler{ //More code button.setOnAction(new EventHandler() { @Override …
Haydos
  • 322
  • 1
  • 8
2
votes
1 answer

java work around to access a variable from anonymous inner class not working

Sorry this is a repeated question , though referring to this post. I have tried to assign a value from anonymous inner class. But it always prints null[Toast.makeText(getBaseContext(),token[0],Toast.LENGTH_SHORT).show();] . Where i am doing wrong in…
Aparichith
  • 1,452
  • 4
  • 20
  • 40
2
votes
4 answers

Object class with anonymous type constructor

I am creating an Object as a class variable with anonymous type. There are no compilation errors. My question is how to use the class? How to call the methods that I am defining? Where is it used actually? public class MyClass { Object o = new…