Tuesday, 4 May 2010

SCJP Sun Certified Programmer By Kathy Sierra and Bert Bates - Part8

Couple of months back i started practicing SCJP Sun Certified Programmer for Java 5 Study Guide By Kathy Sierra and Bert Bates.

Great book written by Kathy Sierra and Bert Bates.

I wanted to share few important quotations found from the eighth chapter.

1) Event handlers are one of the main reasons inner classes were added to the language in the first place.

2) One of the key benefits of an inner class is the "special relationship" an inner class instance shares with an instance of the outer class. That "special relationship" gives code in the inner class access to members of the enclosing (outer) class, as if the inner class were part of the outer class.

3) If we want to Refer the Inner or Outer Instance from within the Inner Class below is the process.

The keyword this can be used only from within instance code which can refer to the inner class.

But what if the inner class code wants an explicit reference to the outer class instance that the inner instance is tied to?

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);
}
}



4) A regular inner class is a member of the outer class just as instance variables and methods are, so the following modifiers can be applied to an inner class:

final

abstract

public

private

protected

static—but static turns it into a static nested class not an inner class.

strictfp

5) Remember that a local class declared in a static method has access to only static members of the enclosing class, since there is no associated instance of the enclosing class. If you're in a static method there is no this, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to instance variables.

6) A method-local inner class can be instantiated only within the method where the inner class is defined. In other words, no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class.

However, the inner class object cannot use the local variables of the method the inner class is in unless it is final.

Reason is The local variables of the method live on the stack, and exist only for the lifetime of the method. You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren't guaranteed to be alive as long as the method-local inner class object, the inner class object can't use them.

7) While an inner class (regardless of the flavor) enjoys that special relationship with the outer class (or rather the instances of the two classes share a relationship), a static nested class does not. It is simply a non-inner (also called "top-level") class scoped within another. So with static classes it's really more about name-space resolution than about an implicit relationship between the two classes.

8) Just as a static method does not have access to the instance variables and non-static methods of the class, a static nested class does not have access to the instance variables and non-static methods of the outer class.

9) Any anonymous interface implementers can implement only one interface.

10) The only modifiers we can apply to a method-local inner class are abstract and final. (Never both at the same time, though.)

11) An anonymous inner class can extend one subclass or implement one interface, Unlike non-anonymous classes (inner or otherwise), an anonymous inner class cannot do both. In other words, it cannot both extend a class and implement an interface, nor can it implement more than one interface.

2 comments:

Anonymous said...

О! Awesome informations, merci beaucoup à l'article de l'écrivain. Il est compréhensible pour moi maintenant, l'efficacité et l'importance est ahurissant. Merci encore une fois et bonne chance!

Anonymous said...

Ich habe einige wunderbare Weisheit.