• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inner Classes Q

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following outline of the declaration of a normal class with an inner class.

public class NormClass {
long startTime ;

public class NestedClass {
// methods and variables of NestedClass
}
// other methods and variables of NormClass
}

Which of the following can be used by a method inside NestedClass to refer to the startTime variable in the enclosing instance of NormClass?

A this.startTime

B NormClass.this.startTime

C this.NormClass.startTime

D startTime

I answered D, but the given answer was B and D. I dont know why B is included???
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from K & B's book:

Referencing the Inner or Outer Instance from Within the Inner Class:
Within an inner class code, the this reference refers to the instance of the inner class, as you�d probably expect, since this always refers to the currently-executing object. But what if the inner class code wants an explicit reference to the outer class instance the inner instance is tied to? In other words, how do you reference the �outer this�? Although normally the inner class code doesn�t need a reference to the outer class,
since it already has an implicit one it�s using to access the members of the outer class, it would need a reference to the outer class if it needed to pass that reference to some other code as follows:

class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + MyOuter.this);
}
}

If we run the complete code as follows:
class MyOuter {
private int x = 7;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + MyOuter.this);
}
}
public static void main (String[] args) {
MyOuter.MyInner inner = new MyOuter().new MyInner();
inner.seeOuter();
}
}
the output is
Outer x is 7
Inner class ref is MyOuter$MyInner@113708
Outer class ref is MyOuter@33f1d7
Inner Classes 9
So the rules for an inner class referencing itself or the outer instance are as follows:
■ To reference the inner class instance itself, from within the inner class code, use this.
■ To reference the �outer this� (the outer class instance) from within the inner class code, use <NameOfOuterClass>.this (example, MyOuter.this).

So, M.Aquino -
Outer class's instance variable can be referenced either directly( Remember nonstatic inner classes have unlimited access to outer class's instance variables.) or through outer class's reference like this . <NameOfTheOuterClass>.this.nameOfTheInstanceVariable.

Hope this helps ...
rgrds,
Jane
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NormClass.this is the syntax for getting a reference to the instance of the outer class that contains the current inner class instance, where the outer class is named NormClass.

Since NestedClass is not static, it can only be instantiated from an instance of the outer class, NormClass. So from the point of view of a NestedClass instance, there must be an enclosing instance of NormClass. The syntax NormClass.this provides code inside NestedClass with a way to access the enclosing instance of NormClass.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi joe
it is possible to have either the outer class static or Inner class static.Is there anyway to have the both classes as static.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can an outer class be static?
 
BWA HA HA HA HA HA HA! Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic