zoukankan      html  css  js  c++  java
  • Nested Classes

    Note:

      A nested class is defined within another class.

    1 class OuterClass{
    2   ...
    3   class NestedClass{...}      
    4 }
    View Code

      There are two types of nested class. One is the static nested class, the other is no-static nested class(inner class).

    1 class OuterClass{
    2 
    3   ...
    4   static class StaticNestedClass{...}
    5 
    6   class InnerClass{...}
    7 
    8 }
    View Code

      A nested class is a member of outer class.

      Static nested classes can not access to outer class's members, While inner classes can do it.

    Why need Nested Classed?

      Group two related classes within one class is more logic,which makes pakages streamlined. Nested classes are "helper classes".

      Inrease encapsulation. Nested classes are hidden from the outside world.

      

    Static Nested Classes

      Like static class methods and variables, a static nested class is related to its outer class,

      which can not refer to its outer class members directly, but through an object reference of its outer class.

      Static nested classes like top-level classes for packaging convenience.

      The syntax of creating a instance of a static nested class in another class is as following

    1 OuterClass.StaticNestedClass  staticNestedClass = new OuterClass.StaticNestedClass()
    View Code

    Inner Classes

      Like instance methods and variables, an inner class is related to an instance of its outer class.

      So, an inner class can directly access its outer class members, but can not have static members itself.

      Instances of an inner class only exist within an instance of its outer class.

      For example

     1 public class OuterClass {
     2 
     3   private String name;
     4   public String getName(){return name;}
     5 
     6   // like a top-level class
     7   static class StaticNestedClass{
     8     // can own static members
     9     static int age;
    10     static void getMessage(){}
    11 
    12     void test(){
    13 
    14       // access outer class members through a object reference of outer class
    15       OuterClass outerClass = new OuterClass();
    16       outerClass.name = outerClass.getName();
    17       outerClass.name = "outer class name";
    18     }
    19   }
    20 
    21   // like an instance method or variable of its outer class
    22   class InnerClass{
    23     // can not have static members
    24 //    static String size;
    25 
    26     void test(){
    27 
    28       // can directly access outer class members
    29       name = getName();
    30       name = "outer class name";
    31     }
    32   }
    33 
    34   public static void main(String[] args) {
    35 
    36     // syntax of creating a static nested class within another class.
    37     OuterClass.StaticNestedClass staticNestedClass = new OuterClass.StaticNestedClass();
    38 
    39     // syntax of creating a inner class within another class.
    40     OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
    41 
    42   }
    43 }
    View Code

     reference from: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

      

      

      

  • 相关阅读:
    setTimeout中0毫秒延时
    javascript中call和apply方法
    javascript闭包
    apns 服务
    新的开始,新的起点
    心情笔记
    如何解决控件附件上传时超大附件无法上传的问题
    BPM实例分享——日期自动计算
    BPM实例分享——金额规则大写
    分享一个程序猿在流程数据查看权限问题的总结
  • 原文地址:https://www.cnblogs.com/yangwu-183/p/13479458.html
Copyright © 2011-2022 走看看