DOC PREVIEW
Penn CIT 591 - Inner Classes

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Inner ClassesInner classesFour kinds of inner classesMember classesMember classes IIAnonymous inner classesExample anonymous inner classStatic member classesLocal classesSummaryThe EndJan 15, 2019Inner Classes2Inner classesAll the classes so far have been “top level”It is possible (and useful) to define a class inside another classThe usual access modifiers (public, protected, private) can be usedInner classes were not in Java 1.0They had to be added in laterAs a result, inner classes are not as well done as some other aspects of the language3Four kinds of inner classesMember classesSimple and usefulAnonymous classesUseful, but syntax is ugly Static member classes (not too useful)Local classes (not too useful)Every class compiles to a separate .class fileInner classes compile to files with a $ in their names4Member classesA member class is an “ordinary” inner class class Outer { int n; class Inner { int ten = 10; void setNToTen( ) { n = ten; } } void setN ( ) { new Inner( ).setNToTen( ); }}5Member classes IIMember classes are often used to handle events: Button b = new Button ("Click Me");b.addActionListener (new Clicker( ));…class Clicker implements ActionListener { … }A member class can access the variables of the enclosing classThis is what makes them so useful!Member classes are very easyDeclare them where you would declare a field or a method6Anonymous inner classesAnonymous inner classes are convenient for short code (typically a single method) b.addActionListener(anonymous inner class);The anonymous inner class can be either: new Superclass(args) { body } or new Interface() { body }Notice that no class name is given--only the name of the superclass or interfaceIf it had a name, it wouldn’t be anonymous, now would it?The args are arguments to the superclass’s constructor (interfaces don’t have constructors)7Example anonymous inner classAn ActionListener is a Java-supplied interface for listening to Buttons and some other thingsThe format (from the previous slide) is new Interface () { body } b.addActionListener (new ActionListener( ) { public void actionPerformed (ActionEvent e) { System.out.println (“Ouch!”);} } ) ;Like member classes, anonymous inner classes have full access to the fields and methods of the containing class8Static member classesstatic class StaticMember { … }A static member class can access only static variables of the outer classA static member class isn't “really” an inner class, but a top-level class that happens to be written inside another classStatic member classes are not too useful9Local classesA local class is a class defined inside a methodLike any other local declarations, the class declaration is available only within that methodHowever, objects created from that local class can “escape” the class by being assigned to nonlocal variablesBecause its instances may exist after the method exits, code in the local class cannot access variables declared in the method unless they are declared finalThis makes them practically uselessThere are many other restrictions on local classes10SummaryMember classesAn ordinary class, just defined within anotherHas full access to the variables of the enclosing classAnonymous classesUseful for short Listeners used in only one place Has full access to the variables of the enclosing classStatic member classesDefined inside another class, but acts like an outer classLocal classesDefined within a methodCan access final variables in the enclosing class11The


View Full Document

Penn CIT 591 - Inner Classes

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Inner Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inner Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inner Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?