Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes Q" Watch "Inner Classes Q" New topic
Author

Inner Classes Q

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
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???


Be Afraid...Be very Afraid...
Jane W pemberley
Greenhorn

Joined: Apr 26, 2005
Posts: 12
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
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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.
Saranyan Narayanan
Ranch Hand

Joined: Mar 12, 2003
Posts: 34
how can an outer class be static?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Classes Q