Anonymous Classes in Java


Anonymous classes enable you to declare and instantiate a class at the same time. They are useful when you have a short implementation that will not be used anywhere else.

Source Code

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello from an anonymous class");
    }
};

new Thread(runnable).start();

Use anonymous classes for implementing interfaces or extending classes in a concise manner, especially for single-use types or when implementing callback methods.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments