The following table shows the types of nested classes( Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)): Types of Nested Classes Type Scope Inner static nested class member no inner [non-static] class member yes local class local yes anonymous class only the point where it is defined yes Nested Classes ( Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035))The Java programming language allows you define a class within another class. Such a class is called a nested class and is illustrated here: class EnclosingClass { ... class ANestedClass { ... } } You use nested classes to reflect and to enforce the relationship between two classes. You should define a class within another class when the nested class makes sense only in the context of its enclosing class or when it relies on the enclosing class for its function. For example, a text cursor might make sense only in the context of a text component. Static Nested ClassesA static nested class is declared inside another class and is declared with the static modifier. Like static methods, a static nested class can only refer directly to static variables and methods. It can refer to instance variables and methods only when given a reference to an object of the enclosing class type. The following code is a rough outline of the way a static class is declared: class NormalClass { // static methods and variables static class NestedClass { // methods and variables of NestedClass } // instance methods and variables of NormalClass } Terms you'll need to understand:- Nested class
- Inner class
- Member class
- Local class
- Anonymous inner class
final local variable
Question 1 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) You need to write code in an applet that will call the submitData method when button1 is clicked. To do this, use addActionListener to connect button1 with an object that implements the ActionListener interface. The only method in this interface has this signature: void actionPerformed( ActionEvent e ); Which of the following anonymous class declarations is the correct way to do this? - A.
button1.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent evt ) { submitData() ; } } ); - B.
button1.addActionListener( new Object implements ActionListener() { public void actionPerformed( ActionEvent evt ) { submitData() ; } } ); - C.
button1.addActionListener( new ActionListener() { submitData() ; } );
Answer A is correct. This block of code will call the submitData method when button1 is clicked. Answer B is incorrect because the Object implements terminology is wrong. An anonymous inner class implementing an interface does extend Object, but the compiler provides the default. Answer C is incorrect because it does not have a method declaration for actionPerformed, the method required by the ActionListener interface.
Question 2 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)Consider the following outline of the declaration of a normal class with an inner class. public class NormClass { public class NestedClass { // methods and variables of NestedClass } // methods and variables of NormClass } Which of the following is the correct way for a method inside NestedClass to refer to the enclosing instance of NormClass? - A. this
- B. NormClass.this
- C. this.NormClass
- D. this.this
- E. this.super
- F. super
Answer B is correct. This is the only correct way to refer to the enclosing instance of an inner class. Answer A refers to the inner class instance itself. Answers C, D, and E won't compile. Answer F is a reference to the parent class of the inner class.
Question 3 of Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035)Which of the following statements is true? - A. An inner class can have the same name as its enclosing class.
- B. An instance of a nonstatic inner class always has an associated instance of the enclosing class.
- C. An anonymous inner class is always assumed to directly extend Object.
- D. An anonymous inner class can be declared as implementing more than one interface.
Only answer B is correct. It is true that an instance of a nonstatic inner class always has an associated instance of the enclosing class. Answer A is incorrect because inner classes are prohibited from having the same name as the enclosing class. Answer C is incorrect because an anonymous inner class can extend any non-final class; however, the declaration syntax does not use the word extends. Answer D is incorrect because the declaration of an anonymous inner class can name only one interface.
|