<< Chapter < Page Chapter >> Page >

An anonymous class cannot define a constructor. However, it can define an instance initializer.

Any arguments that you specify between the parentheses following the superclass name in an anonymous class definition areimplicitly passed to the superclass constructor.

Only one instance of an anonymous class can be created.

As with member classes and local classes, anonymous classes cannot contain static members.

As with local variables and local classes, anonymous classes cannot be declared public , protected , private , or static .

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Java OOP: Anonymous Classes
  • File: Java1640.htm
  • Published: 11/20/13
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

A complete listing of the program discussed in this module is show in Listing 10 .

Figures

Figure 1 - Syntax for anonymous classes.
new className(optional argument list){classBody} new interfaceName(){classBody}

Figure 2 - Program GUI.

Missing image

Figure 3 - Class file names.
GUI$1$BaldButton.class GUI$1.classGUI$2.class GUI$3.classGUI.class InnerClasses08.class
Figure 4 - Screen output.
Local class name: GUI$1$BaldButton Anonymous class B name: GUI$1Anonymous class C name: GUI$2 Anonymous window listener class name: GUI$3buttonA clicked buttonB clickedbuttonC clicked Close button clicked
Figure 5 - Screen output.
Local class name: GUI$1$BaldButton
Figure 6 - Screen output.
Anonymous class B name: GUI$1
Figure 7 - Screen output.
Anonymous class C name: GUI$2
Figure 8 - Screen output.
Anonymous window listener class name: GUI$3
Figure 9 - Screen output.
buttonA clicked buttonB clickedbuttonC clicked Close button clicked

Listings

Listing 1 - The controlling class.
public class InnerClasses08 { public static void main(String[]args){ new GUI();}//end main }//end class InnerClasses08
Listing 2 - Beginning of the GUI class.
class GUI extends Frame{ public GUI(){//constructorsetLayout(new FlowLayout()); setSize(250,75);setTitle("Copyright 2003 R.G.Baldwin");
Listing 3 - Beginning of the BaldButton class.
class BaldButton extends Button{ BaldButton(String text){//constructorenableEvents(AWTEvent.MOUSE_EVENT_MASK); setLabel(text);System.out.println("Local class name: " + getClass().getName());}//end constructor

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