The syntax could also be used in the inner class

This preview shows page 64 - 69 out of 69 pages.

The syntax LinkedList.this.modCount could also be used in the inner class ( ListIterator ) to refer to the instance variable of the outer object ( LinkedList ).
Image of page 64

Subscribe to view the full document.

public class Outer { private int value = 99; public class Inner { private int value; public Inner() { this.value = Outer.this.value + 1; } public int getValue() { return value; } } public Inner newInner() { return new Inner(); } public int getValue() { return value; } }
Image of page 65
Accessing Members of the Outer Object class Test { public static void main( String[] args ) { Outer o = new Outer(); System.out.println( "o.getValue() -> " + o.getValue() ); Outer.Inner i = o.newInner(); System.out.println( "i.getValue() -> " + i.getValue() ); System.out.println( "o.getValue() -> " + o.getValue() ); } }
Image of page 66

Subscribe to view the full document.

Accessing Members of the Outer Object // > java Test // o.getValue() -> 99 // i.getValue() -> 100 // o.getValue() -> 99
Image of page 67
Advanced (optional) example public class A { private int value = 99; public class B { private int value; public B() { this.value = A.this.value + 1; } public class C { private int value; public C() { this.value = B.this.value + 1; } public int getValue() { System.out.println( "A.this.value = " + A.this.value ); System.out.println( "B.this.value = " + B.this.value ); System.out.println( "this.value = " + this.value ); return value; } } } }
Image of page 68

Subscribe to view the full document.

class D { public static void main( String[] args ) { A.B.C abc = new A().new B().new C(); System.out.println( "abc.getValue() -> " + abc.getValue() ); } } // > java D // A.this.value = 99 // B.this.value = 100 // this.value = 101 // abc.getValue() -> 101
Image of page 69
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern