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
9
votes
4 answers

Android and Java: use of runnable

I have read that in Java interfaces can't be instantiated (in the documentation, Interfaces). Runnable, by definition is an interface which should be implemented by some class. But in the following piece of code from one of my Android applications I…
CuriousSid
  • 534
  • 3
  • 11
  • 25
9
votes
2 answers

Can final parameters be qualified in some way to resolve naming conflicts with anonymous class members?

"Why are you doing this what is wrong with you?" notwithstanding, is there any way to accomplish this without changing the final method parameter name? private Foo createAnonymousFoo(final Bar bar) { return new Foo() { private Bar bar =…
Doug Moscrop
  • 4,479
  • 3
  • 26
  • 46
8
votes
3 answers

Optional.orElse does not compile with anonymous types

I encountered a weird problem using Optionals and anonymous classes: public class Foo { interface Bar { } void doesNotCompile() { Optional.of(new Bar() { }).orElse(new Bar() { }); } void…
Mirco
  • 2,940
  • 5
  • 34
  • 57
8
votes
3 answers

Is it possible to create an anonymous class while using reflection?

I would like to be able to implement a method at runtime that is called before an object runs the initializers. This will allow me to set fields that are used during initialization. Here is an example: class A { public A() { initialize(); …
Matt
  • 978
  • 10
  • 24
8
votes
6 answers

Java: where should I put anonymous listener logic code?

we had a debate at work about what is the best practice for using listeners in java: whether listener logic should stay in the anonymous class, or it should be in a separate method, for example: button.addActionListener(new ActionListener() { …
Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
8
votes
2 answers

Java: Defining a generic method inside an anonymous class

The following java code works fine. public static void main(String[] arg){ JPanel p = (new JPanel()); p.add( new Object(){ JButton f(JButton x){ x.setEnabled(false); return x; }}.f(new JButton("B"))…
user7841452
  • 101
  • 6
8
votes
4 answers

Can an anonymous type inherit from another type?

According to the MSDN documentation on the StringComparer.OrdinalIgnoreCase property: The OrdinalIgnoreCase property actually returns an instance of an anonymous class derived from the StringComparer class. Is this a feature I'm unfamiliar…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
8
votes
2 answers

How to exclude an anonyomous class from jacoco?

I am seeing class CacheConfig.new CacheLoader() {...} in my jacoco report. Is there a way to exclude it?
Nelson
  • 2,972
  • 3
  • 23
  • 34
8
votes
6 answers

Java/Android: anonymous local classes vs named classes

I would like to ask what is the good practice on using anonymous classes vs. named inner classes? I am writing an Android application, which includes many UI elements (buttons, text fields, etc). For many of them I need some kind of listeners, so…
Laimoncijus
  • 8,615
  • 10
  • 58
  • 81
8
votes
1 answer

What is the access modifier of an anonymous class's constructor?

NOTE: This is a self-answered question. It may be a very simple one but I thought it would be worth sharing. Suppose I have an anonymous class declaration: MyObject myObj1 = new MyObject() { }; where MyObject is: class MyObject { public…
M A
  • 71,713
  • 13
  • 134
  • 174
8
votes
2 answers

Local variable access to inner class needs to be declared final

I got a problem of local variable access to inner class need to be declared final. It is from method createGrids() -> "squares[i][j] = 0;" that i is a local variable that need to be declared final. I don't know why and I have added final in fields…
yoadle
  • 188
  • 1
  • 2
  • 12
8
votes
5 answers

What does that Java construct do?

I new to java so bear with me if this is a ridiculously simple question but I am curious about this method call which has {code} being taken in - see code below for an example in the method addSelectionListener. What is the purpose of this? I have…
BandyOrc
  • 159
  • 1
  • 8
8
votes
11 answers

Private variables/methods in anonymous class?

I have created an anonymous class in which I declare a few variables and methods. My java teacher tells me to make these private. I don't see how changing the modifier makes any difference since these variables and methods are private to the…
John
  • 171
  • 1
  • 8
8
votes
4 answers

How to use member variables with Interfaces and anonymous implementations

Please check the Java code below: public class Test { public static void main(String arg[]) throws Throwable { Test t = new Test(); System.out.println(t.meth().s); //OP: Old value …
sprabhakaran
  • 1,615
  • 5
  • 20
  • 36
8
votes
1 answer

Serializing anonymous classes with Gson

Is there any reason to why you cant serialize anonymous classes to Json? Example: public class AnonymousTest { private Gson gson = new Gson(); public void goWild() { this.callBack(new Result() { public void…
Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71