(本文参考: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. Struts2 - 传值

    Struts2传值有几种方式: 1)URL parameter 例如配置代码: <action name="login_*" class="com.my.actio ...

  2. lucene、lucene.NET详细使用与优化详解

    lucene.lucene.NET详细使用与优化详解 2010-02-01 13:51:11 分类: Linux 1 lucene简介1.1 什么是luceneLucene是一个全文搜索框架,而不是应 ...

  3. Redis介绍及实践分享

    1.Redis是什么 1)Redis是REmote DIctionary Server的缩写,是一个key-value存储系统 2)Redis提供了一些丰富的数据结构,包括Strings,Lists, ...

  4. Redis 宣言(Redis Manifesto)

    Redis 的作者 antirez(Salvatore Sanfilippo)曾经发表了一篇名为 Redis 宣言(Redis Manifesto)的文章,文中列举了 Redis 的七个原则,以向大家 ...

  5. Puppet Agent/Master HTTPS Communications

    The agent/master HTTP interface is REST-like, but varies from strictly RESTful design in several way ...

  6. for name in loop Shell

    for NAME in joe jane juliedoADDRESS="$NAME@example.com"MESSAGE='Projects are due today!'ec ...

  7. sgu233 little kings

    题目大意: 有n*n的棋盘上放k个国王.国王可以攻击与它相邻的八个格子.现在要使国王不相互攻击,有多少种放置的方案数.一个格子不能放两个国王. n<=10,k<=n*n. 分析:简单的状态 ...

  8. ultraedit15.00.0.1046注册码

      ultraedit注册码,版本:15.00.0.1043·········· 用户名 MAYBELOVE 注册码 LFKKM-KIMMX-OSFEB-PMISO-ELILS-IIIHO-KKHLR ...

  9. Ignoring HTTPS certificates

    Our Development setups won't have valid or trusted certificates. When do you want test our webserver ...

  10. MyEclipse背景色不伤眼+字体大小调节+代码格式化不换行