Certification Summary


Inner classes will show up throughout the exam, in any topic, and these are some of the exam's hardest questions. You should be comfortable with the sometimes bizarre syntax, and know how to spot legal and illegal inner class definitions.

We looked first at "regular" inner classes, where one class is a member of another. You learned that coding an inner class means putting the class definition of the inner class inside the curly braces of the enclosing (outer) class, but outside of any method or other code block. You learned that an inner class instance shares a special relationship with a specific instance of the outer class, and that this special relationship lets the inner class access all members of the outer class, including those marked private. You learned that to instantiate an inner class, you must have a reference to an instance of the outer class.

Next we looked at method-local inner classes—classes defined inside a method. The code for a method-local inner class looks virtually the same as the code for any other class definition, except that you can't apply an access modifier the way you can with a regular inner class. You learned why method-local inner classes cannot use non-final local variables declared within the method—the inner class instance may outlive the stack frame, so the local variable might vanish while the inner class object is still alive. You saw that to use the inner class you need to instantiate it, and that the instantiation must come after the class declaration in the method.

We also explored the strangest inner class type of all—the anonymous inner class. You learned that they come in two forms, normal and argument-local. Normal, ho-hum, anonymous inner classes are created as part of a variable assignment, while argument-local inner classes are actually declared, defined, and automatically instantiated all within the argument to a method! We covered the way anonymous inner classes can be either a subclass of the named class type, or an implementer of the named interface. Finally, we looked at how polymorphism applies to anonymous inner classes: you can invoke on the new instance only those methods defined in the named class or interface type. In other words, even if the anonymous inner class defines its own new method, no code from anywhere outside the inner class will be able to invoke that method.

As if we weren't already having enough fun for one day, we pushed on to static nested classes, which really aren't inner classes at all. Known as static nested classes, a nested class marked with the static modifier is quite similar to any other non-inner class, except that to access it, code must have access to both the nested and enclosing class. We saw that because the class is static, no instance of the enclosing class is needed, and thus the static nested class does not share a special relationship with any instance of the enclosing class. Remember, static inner classes can't access instance methods or variables.




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net