zoukankan      html  css  js  c++  java
  • Inner Class

    Inner Class

     

    Inner classes are class within Class. Inner class instance has special relationship with Outer class. This special relationship gives inner class access to member of outer class as if they are the part of outer class.

    Note: Inner class instance has access to all member of the outer class(Public, Private & Protected)

    Syntax for creating Inner Class


    Type of Inner class

    • Static
    • Method Local
    • Anonymous
    • Other then above these normal inner class

    Normal Inner Class

    Inner classes which are not method local , static or anonymous are normal inner class.

    If you compile above code it will produce two class file.

     Note: You can’t directly execute the inner class’s .class file with java command.

    As it is not static inner class so we can’t use static keyword with it.


    How to access Inner Class

    Inner class can be accessed only through live instance of outer class.

    Within Outer Class

    Outer class can create instance of the inner class in the same way as normal class member.


    From Outside Outer Class

    Create outer class instance and then inner class instance.

    Above code can also be replaced with

    this keyword

    There are some rules associated with this and it refer the currently executing Object. So in case of Inner class “this” keyword will refer the currently executing inner class Object. But to get this for outer class use “OuterClass.this”.

    Modifiers Applied

    Normal inner class will be treated like member of the outer class so it can have several Modifiers as opposed to Class.

    • final
    • abstract
    • public
    • private
    • protected
    • strictfp

    Note: Don’t get confused with the modifiers of Class and Inner Class. They are completely different.


     

    Method Local Inner Class

    When an inner class is defined inside the method of Outer Class it becomes Method local inner class.

    Syntax for Creating Method Local Inner Class

    Now definition of inner class is inside a method in Outer class. Still the instance of the outer class can be created but only after definition of inner class as you can see above.

    Note:
    • Method local inner class can be instantiated within the method where it is defined and no where else.
    • Method local inner class can not use the variable defined in method where it id defined still it can use the instance variable.
    • If method local variable is “Final” method local inner class can use it.(* Now variable is Final)

    Modifiers Applied to Method Local Inner Class

    Method local inner classes are eligible for modifiers like local variable so an method local inner class can have final or abstract.

  • 相关阅读:
    java soket 和nio
    面试题中问到 aop di ioc 怎么回答
    细谈hashmap
    java中length和length()还有size()的区别
    MySQL 中实现可重复读(RR)的原理--MVCC
    关于字节流/字符流操作文件的问题
    MySQL 中索引优化(即避免索引失效)
    MySQL 数据库中索引的实现 和 建立索引的原则
    CAS和ABA问题
    Volatile的简单理解
  • 原文地址:https://www.cnblogs.com/hephec/p/4556585.html
Copyright © 2011-2022 走看看