Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Inner Classes: Understanding Inner and Anonymous Classes in Java with Examples, Study notes of Computer Science

An overview of java inner classes, including kinds of classes (inner/nested), inner class properties, motivating examples, and design solutions. Learn about inner classes, their benefits, and how to use them in java gui programming with examples.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-khg
koofers-user-khg 🇺🇸

10 documents

1 / 22

Toggle sidebar

Related documents


Partial preview of the text

Download Java Inner Classes: Understanding Inner and Anonymous Classes in Java with Examples and more Study notes Computer Science in PDF only on Docsity! 1 Java Inner Classes Department of Computer Science University of Maryland, College Park CMSC 132: Object-Oriented Programming II 2 Overview Java GUI Classes Kinds of Classes (inner/nested) MyIterator Design Inner Classes Anonymous Inner Classes Nested Classes Using inner classes in GUIs 5 Inner Classes Description Class defined in scope of another class Property Can directly access all variables & methods of enclosing class (including private fields & methods) Example public class OuterClass { public class InnerClass { ... } } 6 Inner Classes May be named or anonymous Useful for Logical grouping of functionality Data hiding Linkage to outer class Examples Iterator for Java Collections ActionListener for Java GUI widgets 7 Motivating Example MyList public class MyList { private Object [ ] a; private int size; } Want to make MyList implement Iterable Skipping generic types at the moment Need to be able to return an Iterator 10 MyIterator Design Code public class MyList implements Iterable { private Object [ ] a; private int size; public Iterator iterator() { return new MyIterator(); } public class MyIterator implements Iterator { private int pos = 0; public boolean hasNext() { return pos < size; } public Object next() { return a[pos++]; } … } } 11 Inner Classes Inner class instance Has association to an instance of outer class Must be instantiated with an enclosing instance Is tied to outer class object at moment of creation (can not be changed) MyList MyList MyIterator MyIterator MyIterator 12 Method Resolution When resolving a method call on an unspecified object First see if the method can be resolved on the inner object. If not, see if the method can be resolved on the corresponding instance of the outer object If nested multiple levels, keep on looking 15 Anonymous Inner Class Doesn’t name the class Inner class defined at the place where you create an instance of it (in the middle of a method) Useful if the only thing you want to do with an inner class is create instances of it in one location In addition to referring to fields/methods of the outer class, can refer to final local variables 16 Syntax for Anonymous Inner Classes Use new Foo() { public int one() { return 1; } public int add(int x, int y) { return x + y; } }; To define an anonymous inner class that: Extends class Foo Defines methods one and add 17 MyList Without Anonymous Inner Class Code public class MyList implements Iterable { private Object [ ] a; private int size; public Iterator iterator() { return new MyIterator(); } public class MyIterator implements Iterator { private int pos = 0; public boolean hasNext() { return pos < size; } public Object next() { return a[pos++]; } } } 20 Nested Classes An instance of a nested class does not contain an implicit reference to an instance of the outer class Still defined within outer class, has access to all the private fields Use if inner object might be associated with different outer objects, or survive longer than the outer object Or just don’t want the overhead of the extra pointer in each instance of the inner object 21 Using Inner Classes in GUIs javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndDisplayGUI(); } }); button.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent evt) { System.out.println(“Button pushed”); } }); 22 Using Inner Class in a Count GUI Example: Counter.java
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved