Anonymous Class

Anonymous class is a class without a name! So since there is no name there cannot be a separate file for it. Anonymous classes land up being an inner class from which only a single object is instantiated.

Typically anonymous classes are written when a certain class is only meant for instantiating an object, which is used in one and only one statement, of your entire code. Such scenarios occur in case of graphics programming like Android, Web etc..

For example, when you click on a specific widget on an Android app, the functionality that would be defined for any of the events that could occur on that specific widget, may be encapsulated in an anonymous class. Since this functionality is very specific to that specific widget, the class implementation will never be used anywhere else. For such cases you create anonymous class to keep your code base simple.

Most of the time anonymous classes are used to write implementation classes for listener interfaces that are used in visual programming.

The expression used to create an object from an anonymous class is similar to using a constructor, except you use the Interface name to create the class by providing the body of the class. Remember an Interface cannot be instantiated. However, once you give the class body and use the Interface name in the constructor expression, it would become an anonymous class and an object gets created.

Here is an example where an EditText widget in android is set with an object created from an Anonymous class:


    final EditText text = findViewById(R.id.editText);
    text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    // hide virtual keyboard
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
    });

In the above example TextView.OnEditorActionListener is an interface. The anonymous class body starts from the curly brace next to the name of the interface. The object that is created from this expression is set to setOnEditorActionListener of the EditText object.

If you did not use an anonymous class and went with a regular implementation then this is what you would have done:


    final EditText text = findViewById(R.id.editText);
    MyRegularClass myRegular = new MyRegularClass();
    text.setOnEditorActionListener(myRegular);

 class MyRegularClass implements TextView.OnEditorActionListener {

     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (actionId == EditorInfo.IME_ACTION_GO) {

             InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
             imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
             return true;
         }
         return false;
     }
 }

If this class is never used again other than using it for that specific EditText, then using Anonymous class will keep your code simple

results matching ""

    No results matching ""