(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Nested Classes

class OuterClass {
......
class StaticNestedClass {
......
}
......
class InnerClass {
......
}

定义

nested classes (嵌套类)分为两种:non-staticstatic,前者通常称作inner classes,后者称作static nested classes

作用域与访问范围

两种nested classes都是其enclosing class的内部成员,non-static classes可以引用到包装类的private成员,static classes没有这个能力。两种nested classes都可以被声明为public/protected/private/package private

作用场景(因原文概括性高,直接引用)

  • It is a way of logically grouping classes that only used in one place.
  • It increases encapsulation.
  • It can lead to more readable and maintainable code.

Static Nested Classes

  • static nested classes不可直接调用其外部类的方法/成员,必须通过一个外部类的实例才能访问(在这一点上,static inner class表现的如同一个top-level class
  • 用如下方式创建一个static nested class实例
    OuterClass.StaticNestedClass foo = new OuterClass.StaticNestedClass();

Inner Classes

  • As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.
  • Cause an inner class is associated with an instance, it cannot define any static members itself.
  • An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
  • 用如下方式创建一个inner class实例
    OuterClass.InnerClass innerObject = outerObject.new InnerClass();
  • 有两种特殊的inner classes:local classesanonymous class

Shadowing(不知确切的中文翻译是什么,“遮蔽”?)

原文用一段很简洁的代码讲清了这个问题

public class Main {
public static void main(String[] args) throws Exception {
(new OuterClass()).new InnerClass().printX(2);
System.out.println("FINISH!");
}
} class OuterClass {
int x = 0;
class InnerClass {
int x = 1;
public void printX (int x) {
System.out.println("x=" + x);
System.out.println("this.x=" + this.x);
System.out.println("OuterClass.this.x=" + OuterClass.this.x);
}
}
}

输出:

x=2
this.x=1
OuterClass.this.x=0
FINISH!

Serialization

  • Serialization of inner classes, including local and annonymous classes, is strongly discouraged.
  • 原因是编译器在compile certain constructs时,会产生synthetic constructs:these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code.
  • You may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation.

Java - Nested Classes的更多相关文章

  1. Java Nested Classes(内部类~第一篇英文技术文档翻译)

    鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...

  2. Java中的Nested Classes和Inner Classes

    Java中的Nested Classes和Inner Classes Java有嵌套类(Nested Classes)和内部类(Inner Classes)的概念. 嵌套类(Nested Classe ...

  3. Nested Classes

    http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html package priceton; /* * Copyright (c) ...

  4. Top 15 Java Utility Classes

    In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...

  5. java 错误 classes路径配置错误

    1. 错误显示页 2. 解决步骤 2.1. 查看 root cause 信息 org.springframework.beans.factory.BeanCreationException: Erro ...

  6. Java Inner Classes

    When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...

  7. Summary of java stream classes

    Java’s stream classes are good for streaming sequences of bytes, but they’re not good for streaming ...

  8. Java Date Classes

    References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...

  9. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

随机推荐

  1. css字体文件

    ├── glyphicons-halflings-regular.eot├── glyphicons-halflings-regular.svg├── glyphicons-halflings-reg ...

  2. gcc杂谈

    1. -l选项自动给库文件名增加lib前缀和.a/.so后缀.所以如果你有一个lib叫做libusb.a,那么编译选项是-lusb.另一方面,如果你有一个文件叫做libusb.o(是目标文件而不是库文 ...

  3. (C#) 引用工程中发现有黄色叹号

    一个Project 引用 另外 一个Project 显示黄色叹号,后来发现 后一本Project的build设定为.Net4.5, 前一个为4.0, 将版本改为一致后,问题解决.

  4. Spark工程开发前台技术实现与后台函数调用

    Spark是一个通用的大规模数据快速处理引擎.可以简单理解为Spark就是一个大数据分布式处理框架.基于内存计算的Spark的计算速度要比Hadoop的MapReduce快上50倍以上,基于磁盘的计算 ...

  5. UVA 11137 Ingenuous Cubrency(dp)

    Ingenuous Cubrency 又是dp问题,我又想了2 30分钟,一点思路也没有,最后又是看的题解,哎,为什么我做dp的题这么烂啊! [题目链接]Ingenuous Cubrency [题目类 ...

  6. adb 工具学习

    adb (android debug bridge)简单介绍: 1.adb 是 Android SDK中所带工具.使用adb,可以在PC上操作Android设备或者模拟器 2.主要功能有: 将本地ap ...

  7. redis使用日志(二) 数据存储到redis

    一段简短的代码,来展示如何把爬取内容写到redis里面: #! /usr/bin/env python # -*- coding=utf-8 -*- import requests import js ...

  8. Apache2 CGI demo

    1. 修改 httpd.conf  配置 <IfModule alias_module> ScriptAlias /cgi-bin/ "/usr/local/apache2/cg ...

  9. Eclipse内置Tomcat的配置

    1.首先肯定是得下载J2EE版本的eclipse了,再去Apache Tomcat的官网去下一个Tomcat.都解压到自己想放的目录 2.Eclipse -> Preferences -> ...

  10. mac eclipse 下安装subclipse

    参考 http://www.cnblogs.com/yinxiangpei/articles/3859057.html 推荐安装homebrew 在安装javahl时注意版本对应 http://sub ...