Card Set Information

2012-11-06 17:42:28
Inner Classes

Inner Classes
Show Answers:

  1. what are the four types of inner classes
    • Inner classes
    • Methid Local Inner classes
    • Anonymous inner classes
    • Static nexted classes
  2. What is the relationship between an inner and an outer class?
    • The inner class gets access to the members of the outer class as if they were the members of the outer class. It has access to the private members of the outer class. A regular inner class is one which is not static, method-local and anonylous inner class.
  3. How can you access a inner class.
    • Through an instance of an outer class and not directly. The inner class is a member of the outer class and hence can access the methods of the outer class.
  4. How do you create an instance an inner class?
    • YOU need to have an instance of an outer class.
  5. Eg of an regular inner classs.
    • public class MyOuter{
    • private int x;
  6. public void makeInner(){
  7. Inner in = new Inner();
    • in.seeOuter();
    • }
  8. class Inner{
  9. void seeOuter(){
    • System.out.prinln("x="+x);
    • }
    • }
    • }
  10. Why is Inner object created from a method is correct.
    • because we have the outer object in the method through this.
  11. How do you create inner class object from ouside the outer class instance code.
    • Myouter.MyInner inner = new MyOUter().new MyInner();
  12. Inside the inner class "this" refers to which class?
    • to the inner class
  13. How do you get access to the Outer class's this?
    • Using MyOuter.this
  14. What is method local inner class?
  15. Defining a class instead a method. And after the method closes creating an object of the inner class to access the details of inner class. A method local inner class can be instantiated only inside a method.
  16. can the method local inner class object have access to the local variables of the methods?
    • No. Unless they are marked final. Because the local variables of the method live in a stack and they are blown away once the method is done. But that is nto the case for method local Inner class object it lives in a heap and it might still survive.
  17. what are the only modifiers that you can apply to method local inner class?
  18. abstract and final and not togther.