<< Chapter < Page Chapter >> Page >
  • Member classes
  • Local classes
  • Anonymous classes
  • Nested top-level classes and interfaces

The previous two modules explained member classes and local classes. This module will explain anonymous classes.

What is an anonymous class ?

I'm going to begin my discussion with a quotation from one of my favorite authors, David Flanagan, author of Java in a Nutshell .

"An anonymous class is essentially a local class without a name."

If you have read the previous module, you should know quite a lot about local classes at this point in time. Continuing with Flanagan's words,

"Instead of defining a local class and then instantiating it, you can often use an anonymous class to combine these two steps... an anonymousclass is defined by a Java expression , not a Java statement . This means that an anonymous class definition can be included within a larger Java expression..."

As you will see from the sample program in this module, anonymous class definitions are often included as arguments to method calls.

As is the case for an object of a member class or a local class (discussed in previous modules), an object of an anonymous class must be internally linked to an object of the enclosing class.

Thus, an anonymous class is truly an inner class, because an object of the anonymous class cannot exist in the absence of an object of the enclosing class.

What about an anonymous interface ?

Interfaces defined within classes are implicitly static. This means that they are always top-level. There is no such thing as a member interface, a local interface, or an anonymous interface.

Why use anonymous classes ?

As with local classes, objects instantiated from anonymous classes share many of the characteristics of objects instantiated from member classes.However, in some cases, an anonymous class can be defined closer to its point of use than would be possible with a member class or a local class. Once youbecome accustomed to the somewhat cryptic syntax used with anonymous classes, this can often lead to improved code readability.

Probably the most important benefit of anonymous classes has to do with accessing the members of enclosing classes. Just like with member classesand local classes, methods of an anonymous class have direct access to all the members of the enclosing classes, including private members. Thus the useof anonymous classes can often eliminate the requirement to connect objects together via constructor parameters.

In addition, although not demonstrated in this module, as with local classes, objects of anonymous classes have access to final local variables that are declared within the scope of the anonymous class.

Can be particularly useful when ...

An anonymous class can be particularly useful in those cases where

  • There is no reason for an object of the anonymous class to exist in the absence of an object of the enclosing class.
  • There is no reason for an object of the anonymous class to exist outside a method of the enclosing class.
  • Methods of the object of the anonymous class need access to members of the object of the enclosing class.
  • Methods of the object of the anonymous class need access to final local variables and method parameters belonging to the method in which the anonymous class is defined.
  • Only one instance of the anonymous class is needed.
  • There is no need for the class to have a name that is accessible elsewhere in the program.

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask