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反射并不是什么高深技术,面向对象语言都有这个功能,而且功能也很简单,就是利用jvm动态加载时生成的class对象
    java反射意义
    vs2013中国集
    用户向导页面实现左右滑动的ImageSwitcher
    Java / Android H基于ttp多线程下载的实现
    成语接龙(dfs)
    python 弄github代码库列表
    leetcode先刷_Unique Paths II
    jquery于form正在使用submit问题,未解决
    Vs2012于Linux应用程序开发(2):图案
  • 原文地址:https://www.cnblogs.com/hephec/p/4556585.html
Copyright © 2011-2022 走看看