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.

  • 相关阅读:
    tomcatserver解析(五)-- Poller
    最新版OpenWrt编译教程,解决依赖问题
    操作系统2015(四川大学软件学院)
    Kafka专业监控系统Kafka Eagle:支持kerberos认证,并且对接星环TDH集群
    logstash导出ElasticSearch数据到CSV及同步两套ES的数据研究
    hive通过like方式查询多个值
    hadoop balancer平衡集群各节点数据
    Inceptor命令04-表
    Inceptor命令02-命令使用
    Inceptor命令01-表介绍
  • 原文地址:https://www.cnblogs.com/hephec/p/4556585.html
Copyright © 2011-2022 走看看