Nested classes are further divided into two types:

  1. static nested classes: If the nested class is static, then it’s called static nested class. Static nested classes can access only static members of the outer class. Static nested class is same as any other top-level class and is nested for only packaging convenience.

    Static class object can be created with following statement:

    1
    2
    OuterClass.StaticNestedClass nestedObject =
         new OuterClass.StaticNestedClass();

    静态嵌套类:如果嵌套类是静态的,则称作静态嵌套类。静态嵌套类只能访问外部类的静态成员。

  2. java inner class: Any non-static nested class is known as inner class. Inner classes are associated with the object of the class and they can access all the variables and methods of the outer class. Since inner classes are associated with instance, we can’t have any static variables in them. Object of inner class are part of the outer class object and to create an instance of inner class, we first need to create instance of outer class.

    Inner classes can be instantiated like this:

    1
    2
    OuterClass outerObject = new OuterClass();
    OuterClass.InnerClass innerObject = outerObject.new InnerClass();

There are two special kinds of java inner classes.

  1. local inner class: If a class is defined in a method body, it’s known as local inner class. Since local inner class is not associated with Object, we can’t use private, public or protected access modifiers with it. The only allowed modifiers are abstract or final. A local inner class can access all the members of the enclosing class and local final variables in the scope it’s defined.

    Local inner class can be defined as:

    1
    2
    3
    4
    5
    6
    7
    public void print() {
            //local inner class inside the method
            class Logger {
                String name;
            }
            //instantiate local inner class in the method to use
            Logger logger = new Logger();
  2. anonymous inner class: A local inner class without name is known as anonymous inner class. An anonymous class is defined and instantiated in a single statement. Anonymous inner class always extend a class or implement an interface. Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class. Anonymous inner classes are accessible only at the point where it is defined.
    It’s a bit hard to define how to create anonymous inner class, we will see it’s real time usage in test program below.

Here is a java class showing how to define java inner class, static nested class, local inner class and anonymous inner class.

 
OuterClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.journaldev.nested;
 
 
import java.io.File;
import java.io.FilenameFilter;
 
 
public class OuterClass {
     
    private static String name = "OuterClass";
    private int i;
    protected int j;
    int k;
    public int l;
 
    //OuterClass constructor
    public OuterClass(int i, int j, int k, int l) {
        this.i = i;
        this.j = j;
        this.k = k;
        this.l = l;
    }
 
 
    public int getI() {
        return this.i;
    }
 
 
    //static nested class, can access OuterClass static variables/methods
    static class StaticNestedClass {
        private int a;
        protected int b;
        int c;
        public int d;
 
 
        public int getA() {
            return this.a;
        }
 
 
        public String getName() {
            return name;
        }
    }
 
 
    //inner class, non static and can access all the variables/methods of outer class
    class InnerClass {
        private int w;
        protected int x;
        int y;
        public int z;
 
 
        public int getW() {
            return this.w;
        }
 
 
        public void setValues() {
            this.w = i;
            this.x = j;
            this.y = k;
            this.z = l;
        }
 
 
        @Override
        public String toString() {
            return "w=" + w + ":x=" + x + ":y=" + y + ":z=" + z;
        }
 
 
        public String getName() {
            return name;
        }
    }
 
 
    //local inner class
    public void print(String initial) {
        //local inner class inside the method
        class Logger {
            String name;
 
 
            public Logger(String name) {
                this.name = name;
            }
 
 
            public void log(String str) {
                System.out.println(this.name + ": " + str);
            }
        }
 
        Logger logger = new Logger(initial);
        logger.log(name);
        logger.log("" + this.i);
        logger.log("" + this.j);
        logger.log("" + this.k);
        logger.log("" + this.l);
    }
 
 
    //anonymous inner class
    public String[] getFilesInDir(String dir, final String ext) {
        File file = new File(dir);
        //anonymous inner class implementing FilenameFilter interface
        String[] filesList = file.list(new FilenameFilter() {
 
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(ext);
            }
 
        });
        return filesList;
    }
}

Here is the test program showing how to instantiate and use nested class in java.

NestedClassTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.journaldev.nested;
 
import java.util.Arrays;
//nested classes can be used in import for easy instantiation
import com.journaldev.nested.OuterClass.InnerClass;
import com.journaldev.nested.OuterClass.StaticNestedClass;
 
public class NestedClassTest {
 
    public static void main(String[] args) {
        OuterClass outer = new OuterClass(1,2,3,4);
         
        //static nested classes example
        StaticNestedClass staticNestedClass = new StaticNestedClass();
        StaticNestedClass staticNestedClass1 = new StaticNestedClass();
         
        System.out.println(staticNestedClass.getName());
        staticNestedClass.d=10;
        System.out.println(staticNestedClass.d);
        System.out.println(staticNestedClass1.d);
         
        //inner class example
        InnerClass innerClass = outer.new InnerClass();
        System.out.println(innerClass.getName());
        System.out.println(innerClass);
        innerClass.setValues();
        System.out.println(innerClass);
         
        //calling method using local inner class
        outer.print("Outer");
         
        //calling method using anonymous inner class
        System.out.println(Arrays.toString(outer.getFilesInDir("src/com/journaldev/nested", ".java")));
         
        System.out.println(Arrays.toString(outer.getFilesInDir("bin/com/journaldev/nested", ".class")));
    }
 
}

Here is the output of above program:

1
2
3
4
5
6
7
8
9
10
11
12
13
OuterClass
10
0
OuterClass
w=0:x=0:y=0:z=0
w=1:x=2:y=3:z=4
Outer: OuterClass
Outer: 1
Outer: 2
Outer: 3
Outer: 4
[NestedClassTest.java, OuterClass.java]
[NestedClassTest.class, OuterClass$1.class, OuterClass$1Logger.class, OuterClass$InnerClass.class, OuterClass$StaticNestedClass.class, OuterClass.class]

Notice that when OuterClass is compiled, separate class files are created for inner class, local inner class and static nested class.

Benefits of Java Nested Class

  1. If a class is useful to only one class, it makes sense to keep it nested and together. It helps in packaging of the classes.
  2. Nested classes increases encapsulation. Note that inner classes can access outer class private members and at the same time we can hide inner class from outer world.
  3. Nesting small classes within top-level classes places the code closer to where it is used and makes code more readable and maintainable.

Java内部类、静态嵌套类、局部内部类、匿名内部类的更多相关文章

  1. Java中的嵌套类和内部类

    以前看<Java编程思想>的时候,看到过嵌套类跟内部类的区别,不过后来就把它们的概念给忘了吧.昨天在看<数据结构与算法分析(Java语言版)>的时候,又遇到了这个概念,当时就很 ...

  2. Java基础 -- 嵌套类(非静态嵌套类、静态嵌套类)

    可以将一个类的定义放在另一个类的内部定义,这样的类就被称为嵌套类,包含嵌套类的类被称为外部类(outer class),也可以叫做封闭类. 嵌套类可以分为两种: 静态嵌套类(Static Nested ...

  3. Java学习笔记-嵌套类

    嵌套类 嵌套类有两种类别:static and non-static,分别对应为静态嵌套类和内部类. class OuterClass { ... static class StaticNestedC ...

  4. 【java开发系列】—— 嵌套类与内部类

    嵌套类与内部类在java中使用的很广泛,为了避免难以理解,这里用了几个小例子,说明它的用法. 嵌套类与内部类的结构如下图 静态嵌套类 静态嵌套类,是一种在类之外声明的嵌套类,由于是静态的,所以不经过初 ...

  5. JAVA 嵌套类和内部类

    一.什么是嵌套类及内部类?  可以在一个类的内部定义另一个类,这种类称为嵌套类(nested classes),它有两种类型:  静态嵌套类和非静态嵌套类.静态嵌套类使用很少,最重要的是非静态嵌套类, ...

  6. 分析java 嵌套类与内部类

    一.什么是嵌套类及内部类?     可以在一个类的内部定义另一个类,这种类称为嵌套类(nested classes),它有两种类型:静态嵌套类和非静态嵌套类.静态嵌套类使用很少,最重要的是非静态嵌套类 ...

  7. 深入理解java嵌套类和内部类

    一.什么是嵌套类及内部类 能够在一个类的内部定义还有一个类.这样的类称为嵌套类(nested classes),它有两种类型:静态嵌套类和非静态嵌套类.静态嵌套类使用非常少,最重要的是非静态嵌套类,也 ...

  8. (十二)java嵌套类和内部类

    嵌套类和内部类:在一个类里边定义的类叫做嵌套类,其中没有static修饰的嵌套类是我们通常说的内部类,而被static修饰的嵌套类不常用.有的地方没有嵌套类和内部类的区分,直接是嵌套类就称作内部类,没 ...

  9. “全栈2019”Java第一百一十章:局部内部类与匿名内部类区别详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

随机推荐

  1. java学习材料

    java资料大全 http://zz563143188.iteye.com/ it男视野扩展资料 HTTP://WLSAM168.400GB.COM 最全 spring mvc http://jinn ...

  2. JQuery实现页面企业广告图片切换和新闻列表滚动效果

    最近用到一个页面上图片左右切换和新闻列表滚动呈现的效果,整理如下: 前段代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  3. LIS 最长递增子序列

    一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...

  4. FtpWebRequest FTP异步下载、异步上传文件

    异步下载: public interface IPrimaryKey<T> { T GetKey(); } public class DownloadInfo : IPrimaryKey& ...

  5. IDEA SDK(Software Development Kit) 介绍

    如上图标注 1 所示,IntelliJ IDEA 支持 6 种 SDK.最常用的就是 JDK 和 Android SDK,其中在创建 Android SDK 的时候如果你没有先配置一个 JDK 的话, ...

  6. java collections读书笔记(9)collection框架总览(2)

    框架算法: 1)collection接口 add()  Adds an element to the collection.addAll()  Adds a collection of element ...

  7. Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-1

    学习目标 一.进一步学习Swift的游戏制作 二.掌握SKNode,SKSpriteNode的运用 三.了解SpriteKit的物理系统 四.掌握动作(SKAction)的运用 在这一章,我们要通过制 ...

  8. lcd 图片

    硬件平台:mini2440 软件环境:UCOS2 .ADS1.2 . LCD彩色图片转换工具BMP_to_H工具bmp2h LCD彩色图片转换工具BMP_to_H工具文件夹下的使用说明 在S3C241 ...

  9. Mac 显示和隐藏 隐藏文件

    控制台运行: //显示 defaults write com.apple.finder AppleShowAllFiles -bool true //隐藏 defaults write com.app ...

  10. CCF真题之窗口

    201403-2 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示 ...