Sunday, February 18, 2018

Java Anonymous Class





anonymous inner class
anonymous inner class is used only when a new object created, we add new function or properties to the class constructor.

Try override functions by using anonymous on fly!

class B1{
    int c = 1;
    void print() {
        System.out.println(c);
    }
}


https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html


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. Use them if you need to use a local class only once.
Class anonymousClassInstance  = new Class() { new fields or new methods}   // <== Anonymous classes 
public class AnonymousClassTest { public static void main(String[] args) { B1 b1 = new B1() { int a = 10; int b = 90; int getAB() { return a + b; } @Override void print() { System.out.println(a + " " + b + " " + c); } @Override public String toString() { return a + " " + getAB(); } }; b1.print(); System.out.println(b1); // Note: good idea is to override base class methods // the following won't work, and do not compile. //System.out.println(b1.c +" " + b1.a + " " + b1.b + " " + b1.getAB()); return; }
}



The anonymous class expression consists of the following:
  • The new operator
  • The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.
  • Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.
  • A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.
Because an anonymous class definition is an expression, it must be part of a statement. In this example, the anonymous class expression is part of the statement that instantiates the frenchGreeting object. (


Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class

Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
  • An anonymous class has access to the members of its enclosing class.
  • An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
  • Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing scope that have the same name. See Shadowing for more information.
Anonymous classes also have the same restrictions as local classes with respect to their members:
  • You cannot declare static initializers or member interfaces in an anonymous class.
  • An anonymous class can have static members provided that they are constant variables.
Note that you can declare the following in anonymous classes:
  • Fields
  • Extra methods (even if they do not implement any methods of the supertype)
  • Instance initializers
  • Local classes
However, you cannot declare constructors in an anonymous class.

No comments:

Post a Comment

java special for collection size, array size, and string size

Size: For Collections (eg: Map, List, etc ): usually it use collection.size(), eg         Map<Character, Integer> map ...