Charanor
Charanor

Reputation: 860

What do you call an object that defines its methods/fields when it is instantiated?

What it called when you have a class body after you create an object and what it's useful for?

Example:

public MyObject myObject = new MyObject() /*from here*/{
    void myMethod() {
        //code
   }
};/*to here*/

Because usually it just looks like this:

public MyObject myObject = new MyObject();

Upvotes: 1

Views: 66

Answers (1)

Buddy
Buddy

Reputation: 11028

They are called Anonymous Classes. Check out the docs: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

It's a way to derive from a class in just the place that you want to use it.

Upvotes: 5

Related Questions