21Iterator - ITI 1121. Introduction to Computing II *...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 20, 2011 Abstract • Iterator (part II) – Inner class – Implementation: “fail-fast” * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Implementation -2-: using an inner class We’ll use an inner class to create the iterators: • An instance of an inner class cannot exist on its own, it requires an instance of the outer class (the converse is not true); • An instance of an inner class has access to the instance variables (and methods) of the (outer) object that created it; class Outer { class Inner { } Inner newInner() { return new Inner(); } } ⇒ think about instance variables and methods. Getting in Touch with your Inner Class (web resource) www.javaranch.com/campfire/StoryInner.jsp Inner class Let’s call a non-static nested class an inner class. • An instance of an inner class cannot exist on its own, it requires an instance of the outer class (but an instance of the outer class does not necessarily require an instance of an inner class); • An instance of an inner class has access to the instance variables (and methods) of the (outer) object that created it; class Outer { class Inner { } } ⇒ Think about instance variables and methods. public class Outer { class Inner { Inner() { System.out.println( "* new instance of Inner class *" ); } } public Outer() { System.out.println( "* new instance of Outer class *" ); } } class Test { public static void main( String args ) { Outer o = new Outer(); } } java Test simply prints “* new instance of Outer class *”, i.e an instance of the Outer class can exist without the need to create an instance of the inner class (this is not automatic). public class Outer { class Inner { Inner() { System.out.println( "* new instance of Inner class *" ); } } public Outer() { System.out.println( "* new instance of Outer class *" ); } public static void doesNotWork() { Inner i = new Inner(); } } Outer.java:11: non-static variable this cannot be referenced from a static context Inner i = new Inner (); ^ ⇒ An instance of an inner class can only be created by an instance of the outer class! public class Outer { class Inner { Inner() { System.out.println( "* new instance of Inner class *" ); } } Inner i; public Outer() { System.out.println( "* new instance of Outer class *" ); i = new Inner(); } } class Test { public static void main( String args ) { Outer o = new Outer(); } } > java Test * new instance of Outer class * * new instance of Inner class * public class Outer { private class Inner { private Inner() { System.out.println( "* new instance of Inner class *" ); System.out.println( "value = " + value ); } } private Inner o; private String value; public Outer() { System.out.println( "* new instance of Outer class *" ); value = "from the instance variable of the outer object"; o = new Inner(); } } class Test { public static void main( String args ) { new Outer(); } } > java Test...
View Full Document

This note was uploaded on 03/02/2012 for the course ITI 1121 taught by Professor Samaan during the Winter '10 term at University of Ottawa.

Page1 / 69

21Iterator - ITI 1121. Introduction to Computing II *...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online