🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › anonymousclasses.html
Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
While local classes are class declarations, anonymous classes are expressions, which means that you define the class in another expression. The following example, HelloWorldAnonymousClasses, uses anonymous classes in the initialization statements of the local variables frenchGreeting and spanishGreeting, but uses a local class for the initialization of the variable englishGreeting:
🌐
W3Schools
w3schools.com › Java › java_anonymous.asp
Java Anonymous Class
An anonymous class is a class without a name. It is created and used at the same time.
People also ask

Are there any limitations when using anonymous classes with Java's functional interfaces?
Although anonymous classes can implement functional interfaces, they lack the conciseness of lambda expressions. Lambdas are preferred for functional interfaces because they provide a cleaner and more compact syntax, whereas anonymous classes tend to introduce unnecessary boilerplate.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › anonymous class in java
A Complete Guide to Anonymous Class in Java with Examples
Can an anonymous class access private members of the enclosing class?
Yes, anonymous classes can access the private members of the enclosing class, as long as they are within the same scope. This feature is helpful when you need to create a concise class implementation that interacts closely with the enclosing class.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › anonymous class in java
A Complete Guide to Anonymous Class in Java with Examples
How can anonymous classes help in GUI programming?
In GUI programming, anonymous classes are frequently used to handle events like button clicks or mouse movements. They allow you to define event listeners directly where needed, minimizing the boilerplate and keeping event handling logic close to the GUI component.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › anonymous class in java
A Complete Guide to Anonymous Class in Java with Examples
🌐
GeeksforGeeks
geeksforgeeks.org › java › anonymous-inner-class-java
Anonymous Inner Class in Java - GeeksforGeeks
July 23, 2025 - For example, we know that we can create a thread by extending a Thread class. Suppose we need an immediate thread but we don't want to create a class that extends Thread class all the time.
🌐
Programiz
programiz.com › java-programming › anonymous-class
Java Anonymous Class
Inside an anonymous class. In the above example, we have created an anonymous class that implements the Polygon interface.
🌐
Baeldung
baeldung.com › home › java › core java › anonymous classes in java
Anonymous Classes in Java | Baeldung
June 11, 2024 - We may instantiate an anonymous class from an interface as well: Obviously, Java’s interfaces have no constructors, so the parentheses always remain empty.
🌐
Tutorialspoint
tutorialspoint.com › java › java_anonymous_class.htm
Java - Anonymous Classes
You can use an anonymous inner class to extend a class in Java. In the following example,we're using a new keyword to create an object of the anonymous inner class that has a reference of parent class type.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › anonymous class in java
A Complete Guide to Anonymous Class in Java with Examples
March 6, 2025 - Anonymous classes are often used to provide a quick implementation for interfaces. Let’s see an example with the Runnable interface. Java Anonymous Class Example: Using Anonymous Class with an Interface
🌐
DEV Community
dev.to › satyam_gupta_0d1ff2152dcc › java-anonymous-class-explained-no-more-confusion-5987
Java Anonymous Class Explained: No More Confusion - DEV Community
October 20, 2025 - Example 1: The Classic Thread Starter Remember how you start a new thread? You need a Runnable. Instead of creating a separate class MyRunnable implements Runnable, you can use an anonymous class.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › how-anonymous-classes-work-internally-in-java-75c961b32237
How Anonymous Classes Work Internally in Java | Medium
March 21, 2025 - Anonymous classes don’t have named constructors, so Java uses instance initialization blocks instead. Here’s an example showing how that works:
🌐
Edureka
edureka.co › blog › anonymous-class-in-java
How to Implement Anonymous Class in Java | Edureka
June 17, 2021 - As in above example, it is implementing the interface. It contains parentheses just like normal classes in order to pass arguments to the constructor. Contains body which contains method declarations. Declarations are not allowed. Anonymous class should be part of statements.
🌐
AlgoMaster
algomaster.io › home › java › anonymous classes
Anonymous Classes | Java | AlgoMaster.io
June 2, 2026 - If the local variable could change after capture, the copy inside the anonymous class would silently drift away from the original. Java avoids that confusion by forbidding the case entirely. A working example: a one-off button-style listener that prints which customer placed an order.
Top answer
1 of 5
2

The most minimal example:

interface Foo {}
public static void main (String[] args)
{
    Foo foo = new Foo() {};
}

Literally a declaration of an interface, and then usage as an anonymous class with no additional declarations.

Practically speaking, it does nothing. However, as we add bits in:

interface Foo {
    public void bar();
}
public static void main (String[] args) throws java.lang.Exception
{
    Foo foo = new Foo() {
        public void bar() {
            System.out.println("Hello");
        }
    };
}

It becomes a full-fledged helper class for our method.

The most common use for early/mid level programming would be overriding Listeners to do specific actions. We know the Listener is listening for something, and we want it to do something as a result of the Listener, so we craft the Listener and say "Do this when you are triggered."

Here's the example of a really complex ActionListener tutorial: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

But typically, if it's something that's mundane like "run a method on click", you'll use an anonymous in-line declaration that just calls a method for you.

2 of 5
1

I suppose the "absolute minimal requirement" to create an anonymous class is to have a place in your code that requires an instance of a non-final class or interface of some kind.

Meaning, if I have a method in MyClass:

public static void gimmeMyObject(MyObject c)

I can define an anonymous class that extends MyObject as long as MyObject is not final:

//Somewhere in a method
MyClass.gimmeMyObject(new MyObject() {
      public String myMethod() {
           return "I'm anonymous";
      }
});

That anonymous class will be passed in as a MyObject.

However, I could not do this if the method required a String or Integer, for example, because those are final classes.

For the above example, the non-anonymous class would translate to:

public class MyAnonObject extends MyObject { //In actuality, an anonymous class doesn't have a name, though.

     public String myMethod() {
           return "I'm anonymous";
     }
}
🌐
CodingNomads
codingnomads.com › java-anonymous-inner-class
Primer: What is an Anonymous Class in Java
Even though that may seem confusing, below, you're going to review what you already know about interfaces and see how the anonymous inner class can save time and code. ... In order for you to use this interface, you would typically create a new class that implements this interface. For instance: public class ExampleClass implements ExampleInterface { @Override public double calculate(double a, double b) { if (a > b){ return a; } else { return b; } } }
🌐
Medium
medium.com › @shubhranshugupta › understanding-anonymous-classes-in-java-3ffc109d5023
Understanding Anonymous Classes in Java | by Shubhranshu Gupta | Medium
June 15, 2024 - ... import java.util.*; public class AnonymousClassExample { public static void main(String[] args) { List<String> names = Arrays.asList("Ram", "Shyam", "Mohan"); // Using an anonymous class to create a custom Comparator Collections.sort(names, ...
🌐
Judahstutorials
judahstutorials.com › java-anonymous-class-primer
Java Anonymous Class Primer – Original – Judah's Tutorials
For further information, see the Comparator Javadoc Page. List.sort(Comparator<T> cmp) is a method declared by the List<T> interface that sorts a list according to the comparison rule implemented by cmp. The explicit declaration of ReverseSorter in the above example can be replaced with an anonymous class ...
Top answer
1 of 7
5

Your question is a little confusing, but it sounds like you want a class that both extends Thread and implements some interface, but which can't be accessible outside of the method it's being used by (which would rule out even private nested classes). In that case use a local class:

void myMethod() {
    class Impl extends Thread implements SomeInterface {
        @Override
        public void someInterfaceMethod(String s){ }
    }
    new Impl().someInterfaceMethod("data");
}

EDIT: In response to your update, you can naturally give the local class a constructor and pass in data, or just let it close over final variables in the outer method.

2 of 7
3

if i can't syntactically do this, can anyone explain the logic/implementation design that of why this would never work?

I can explain. If you have an expression foo.bar(...) in Java, the compiler must make sure that the static (compile-time) type of the expression foo supports the method bar(). In your first case, the expression on the left is the anonymous class creation expression, so its static type is the anonymous class which has the method.

However, in your second case, the expression on the left is a variable. The static type of a variable is the type that the variable was declared with. In Java, variables must be declared with an explicit type (no var or auto like some other languages). Therefore, its static type must be a named type. Thread and no other named type supports the method NoNoYouCantCallMe(); only the anonymous class supports this method, and you can't declare a variable of anonymous class type. That's why it is not syntactically possible.

However, I would like simply like to pass some data to this anonymous class only once (ideally the constructor, but you will notice that you can't actually override the constructor in Thread()).

This is simple. You don't need to explicitly pass data into the anonymous class, because inside anonymous classes and local classes you automatically have access to final variables from the surrounding scope. So instead of passing data in, you just directly use data from the outside.

final String someData = "blah";

Thread t = new Thread() {
    public void run() { doStuffWith(someData); }
};
🌐
WsCube Tech
wscubetech.com › resources › java › anonymous-class
Java Anonymous Class: With Examples, Uses, Features
August 12, 2026 - Learn Java anonymous classes with practical examples. Understand their syntax, working, uses, implementation, and how to create anonymous classes in Java.
🌐
Scaler
scaler.com › home › topics › anonymous class in java
Anonymous Class in Java - Scaler Topics
April 8, 2024 - An Anonymous Class is a nested class without a name. It must be defined within another class, which is why it is also known as an Anonymous Inner Class in Java.
🌐
Javatpoint
javatpoint.com › anonymous-inner-class
Java Anonymous Inner Class - javatpoint
Java Program to swap two string variables without using third or temp variable.