eric_the_animal
eric_the_animal

Reputation: 432

What is the terminology for this Java code structure?

I have been using this more frequently now in Java but I don't know what it is called. Essentially there is a function which takes a type as a param and within the function there are overridden functions/methods that can be used. These functions can be executed within functions that are exposed by the main class. For example:

public class Whatisthis {

    private OtherLibsomeCallback theCallback;

    public void dothatthang{

        //What do you call this kind of code structure?
        someCallback(window, theCallback = new OtherLibsomeCallback() {

            @Override
            public void invoke(long a, double b) {
                // TODO Auto-generated method stub
                globalVar = a + b;
            }
        });
    } 
}

Upvotes: 1

Views: 84

Answers (3)

Prashant
Prashant

Reputation: 2614

This Is Anonymous inner class

which can be declare in several ways. its mostly use when you want to override some feature of base class/interface (mostly abstract class/ interface). its giving pure inheritance feature. and its also declared and instantiated at the same time as @Abhishek said.

you can also use anonymous inner class in method's call as :

object.someMethod(new Object(){
    // you can override method here
  });

Upvotes: 0

Abhishek
Abhishek

Reputation: 878

This is called as the Anonymous class.

This is from the Oracle docs:

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name.

Upvotes: 2

Sanjay Rabari
Sanjay Rabari

Reputation: 2081

You can say it short hand coding techniques.

The code is using Anonymous class as argument to function.

Upvotes: 0

Related Questions