Docsity

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

Toggle sidebar
bg1
1
Java Inner Classes
Department of Computer Science
University of Maryland, College Park
CMSC 132:
Object-Oriented Programming II
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

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!

Java Inner Classes

Department of Computer Science

University of Maryland, College Park

CMSC 132:

Object-Oriented Programming II

Overview

Java GUI Classes Kinds of Classes (inner/nested) MyIterator Design Inner Classes Anonymous Inner Classes Nested Classes Using inner classes in GUIs

Kinds of Inner/Nested Classes

Inner class

Defined inside another class But each instance of an inner class is transparentlyassociated with an instance of the outer class Method invocations can be transparently redirectedto outer instance

Anonymous inner classes

Unnamed inner classes

Nested class

Defined inside another class Has access to private members of enclosing class But just a normal class

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 {

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

MyIterator Design

public class MyIterator implements Iterator {

private MyList list;private int pos;MyIterator(MyList list) {

this.list = list;pos = 0; } public boolean hasNext() {

return pos < list.

size

} public Object next() {

return list.

a[pos++];

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++]; }

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

Creating/Referring to Inner Classes Assume class A defines an inner class B Inside instance methods of A, just use B as thetype of references to the inner class and usenew B(…) to create instances

Newly created B object associated with A objectreferenced by this

Outside of A, use A.B to name the inner class If you need to create an instance of Bassociated with a specific A object a, outside ofan instance method on a

Use a.new B() It is

very

rare for you to need to do this

Accessing Outer Scope

Code^ public class OC {

// outer class

int x = 2;public class IC {

// inner class

int x = 6;public void getX() {

// inner class method

int x = 8;System.out.println(

x

// prints 8

System.out.println(

this.x

// prints 6

System.out.println(

OC.this.x

// prints 2

Syntax for Anonymous Inner Classes

Usenew 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

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++]; }

Nested Class

Declared like a standard inner class, exceptyou say “static class” rather than “class”. For example:class LinkedList {

static class Node {

Object head;Node tail;

} Node head;

Nested Classes

An instance of a nested class does not containan implicit reference to an instance of the outerclass Still defined within outer class, has access toall the private fields Use if inner object might be associated withdifferent outer objects, or survive longer thanthe outer object

Or just don’t want the overhead of the extra pointerin each instance of the inner object