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

JDK compiler optimize use of anonymous classes with no instance variables?

I was curious, I see this kind of thing a lot: Arrays.sort(array, new Comparator() { public int compare(Integer a, Integer b) { return Math.abs(a) < Math.abs(b); } }); since the anonymous class created here has no instance…
at.
  • 50,922
  • 104
  • 292
  • 461
5
votes
2 answers

Why can't I use .this in anonymous class?

I recently use this code, and realize that in anonymous class, I can't access the instance by .this, like this: Sprite sprFace = new Sprite() { @Override protected void onManagedUpdate(float pSecondElapsed) { runOnUpdateThread(new…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
5
votes
1 answer

Local AtomicReference and array with lambda

I try change and get local variable with lambda. I know I should use effectively final for local variables in lambda. When I use AtomicReference local variable changing failed: public class Lamb { public static void main(String[] args)…
Abdullajon
  • 366
  • 3
  • 12
5
votes
1 answer

Is lambda in fact an anonymous class?

I am reading Effective Java and wondering about differences between lambda and anonymous class. I know that lambda can only be used with interfaces with single method i.e. Functional Interfaces and in lambda you cannot obtain reference to itself so…
5
votes
1 answer

Why does this.getClass give it's own class name rather than Anonymous class name?

I have created anonymous class by implementing interface I inside public static void main() method. So, by java 8 for the abstract method test(), the implementation is provided from imple() method of class C. So, inside public static void main()…
5
votes
1 answer

Kotlin: Referencing anonymous object from itself / inside (via this)

TL;DR These object : someClass{ } anonymous objects can't access itself via this (which results the outer object). How can I access it? Longer explanation: For my Fragment I need a PreDrawListener. I call this in onCreateView. When executing, I…
Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
5
votes
1 answer

Find all anonymous classes throughout my project in IntelliJ

Is there a way for IntelliJ to locate all the places in my code that define an anonymous class? I am asking just about finding, not changing. I am not asking about converting to lambdas, lambdas are irrelevant here.
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
5
votes
3 answers

Calling Overridden Default Method from Anonymous Inner Class

Consider this code: interface A { default void doA() { System.out.println("a"); } } interface B { void doB(); } class Test implements A { @Override public void doA() { // Works B b = () ->…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
5
votes
1 answer

Implementing Interface anonymously in Kotlin results in "has no constructors" error

I'm trying to use a SurfaceView in Android to hold a Camera preview. The documentation tells me that I need to call startPreview in the surfaceCreated callback for the surface holder. I'm trying to set the callback like…
5
votes
0 answers

Verifier rejected Lambda due to bad method

In an Android project I tried using the Java 8's new lambda feature so I modified an existing functioning anonymous inner class into lambda. However, the app crashes when the code is executed with the following error message: 11-25 11:20:33.485…
Miloš Černilovský
  • 3,846
  • 1
  • 28
  • 30
5
votes
2 answers

how to write lambda expression for the abstract class

I have abstract class which have two methods as follows: public abstract class SessionExecutionBody { public Object execute() { executeWithoutResult(); return null; } public void executeWithoutResult() {} } and I implement the…
Devendra
  • 1,864
  • 6
  • 29
  • 49
5
votes
5 answers

How to check if Iterator.next() is == null?

How do I check if the next element in the list is null ? while(it.hasNext()){ System.out.println(it.next()+"\n"); } this is how I tried, but when the last element is null it prints it as null. I tried to change it …
Lazlow
  • 63
  • 1
  • 2
  • 6
5
votes
1 answer

PHP anonymous class without instance

Is there a way of declaring an anonymous class that has no instance? I'd like to do something like this: $myclass = (class { public $a; })::class; $myobject = new $myclass; This is something you can do with named classes, but the above code…
balping
  • 7,518
  • 3
  • 21
  • 35
5
votes
2 answers

Can I create an anonymous on-the-fly class (implementation of an interface) in C++

In C++, can I create an implementation of an interface on the fly (which ideally binds local-scope variables.) Not sure how to explain it better, so I will put down what I would like the code to look like (roughly): // Given the following: class…
nappyfalcon
  • 909
  • 1
  • 10
  • 17
5
votes
5 answers

What is the preferred way to organize callbacks?

In my Android project, I define a few callbacks to operate on button clicks, connectivity events, or UI events such as Dilaog.onShow(). For demo purposes, I have chosen a Runnable interface that must be launched from some Activity code. With Java, I…
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307