In the following example, how can i invoke the method g2 of the anonymous class? can't think of a specific case that it would be absolutely useful. and i'm aware that anonymous classes are for "on-the-fly" use. however, wondering.

If i can't invoke it, what's the use of being able to define it (if any-- other than being a helper to other methods of the anonymous class itself) in the anonymous class?

no, you cannot call g2. actually, anonymous class in java serves as a short hand to implement an instance of an interface only used in one place. with specified interface, the method you want to be called should be specifically defined by that interface.

the reason to allow you define non-interface method is because designers considered the case when you want to implement helper method.

It's worth noting that anonymous classes need not be declared as an interface implementation. They can be declared as any non-primitive type, all the way up to Object.
–
Mike StrobelAug 22 '14 at 19:52

If i can't invoke it, what's the use of being able to define it (if any-- other than being a helper to other methods of the anonymous class itself) in the anonymous class?

Since they can only be referenced from within the anonymous type itself, they are of no use outside of it. So, as you say, they may be used to organize the logic within the class, but that's about it. The only exception is the edge case that @bcsb1001 describes, in which you invoke the method directly on the anonymous object creation expression (and not the variable to which it is assigned).

the below worked...

That's because the type of the expression new HelloWorld() { ... } is the type of the anonymous class. Since the creation expression has the actual anonymous class type, you can use it to access any members it declares. However, since the class is anonymous, it has no name, so you cannot declare a variable of the concrete anonymous type. The closest you can get is declaring a variable of HelloWorld. Since the variable is declared as HelloWorld, you can only use it to access members declared on HelloWorld or one of its supertypes.

If Java added support for inferred types in declarations, you could write something like var g = new HelloWorld() { ... }; (C# style) or auto g = new HelloWorld() { ... }; (C++ style), and the type of g would be inferred from the assignment. That would allow you to capture the anonymous type without needing a type name. However, Java has no such capabilities at this time.