Search your topic in Java2share
Home > FAQs
eXTReMe Tracker


Inner Classes FAQs



6. What is difference between anonymous inner class and general class?
  • A general class can extend only one class at a time of course inner class can extend only one class at a  Time.
  • A general class can implement any no of interfaces at a time but a anonymous inner class can
  • implement only one interface at a time
  • A general class can extend a class and can implement an interface simultaneously but an

    anonymous inner class can extend a class or can implement an interface one at a time but not

    both simualtaneously 
     

7. What is difference between normal inner class and static nested class?

       

Normal Inner Class Static Nested Class
  1. Inner class object always associated with outer class object ie without existing outer class object    there is no chance of existing inner class object.
  1. Nested class object never associated with
       outer class object , ie  without existing outer class object inner class object can exist
  1. Inside normal inner class we cant declare static members.
  1. Inside static nested class can
         declare static members
  1. We cant place main method in normal inner class and hence innocation of inner class directly from   command prompt is not possible.
  1. We can place main method in static nested class and    hence  innocation of nested class directly from command prompt is possible
  1. From normal inner class we can access both static and non static members of outer class.
  1. From static nested class we can access only static member of outer class

8.What is static nested calss?why the term nested instead of inner in static nested class?

      Some times we can declare inner class with static modifier such type of inner  class are called static

       nested classes.the term nested instead of static because without existing outer class object inner

       class object can exist. 

       Example

      Class outer{

      Static class Nested{

            Public static void main(String[] args){

                  System.out.println(“nested class main()”);

            }

      }

      Public static void main(String[] args){

            System.out.println(“outer class main()”);

      }


  • Java Outer

    O/P

    Outer class main() 

  • Java Outer$Nested

      O/P

    Nested class main() 
     
     

9. Inside inner class is it possible to declare main()?

          No it is not possible to declare main () inside inner class but in static nested class it is possible for 

        Example refer above code