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

 

  

  

  

posted @ 2020-08-11 20:55  deep-thinking  阅读(106)  评论(0编辑  收藏  举报