One way declared types in Java differ from one another is whether the type is a class (which includes enums) or an interface (which includes annotation types). An independent property of a type is its relation to the surrounding lexical context.
top level class does not appear inside another class or interface. If a type is not top level it is nested. However, there are a number of distinct ways a type can be nested. First, a type can be a member of another type; a member type is directly enclosed by another type declaration. All member types have names; however, some member types are inner classes and others are not. If a type is explicitly or implicitly static, it is not an inner class; all member interfaces are implicitly static.
 
Inner classes also include local classes, which are named classes declared inside of a block like a method or constructor body, and anonymous classes, which are unnamed classes whose instances are created in expressions and statements. Anonymous classes are used to implement specialized enum constants. Inner classes have access to instance variables of any lexically 词汇方面 enclosing instance; that is, the fields of the object an inner class is created in reference to. However, not all inner classes have enclosing instances; inner classes in static contexts, like an anonymous class used in a static initializer block, do not.
The Venn diagram below shows how these distinctions relate and combine; in particular, member-ness and inner-ness are not orthogonal properties.

用下面的一幅图来进一步理解它们之间的关系,如下:

注意:

(1)除了static member class可以使用static field与static method、static block外,其它都不可以用static修改。

(2)A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.

(3)Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static.This implies that it is impossible to define a local (§14.3) enum, or to define an enum in an inner class (§8.1.3).

(4)The access modifier public (§6.6) pertains only to top level classes (§7.6) and to member classes (§8.5), not to local classes (§14.3) or anonymous classes (§15.9.5).

(5)The access modifiers protected and private (§6.6) pertain only to member classes within a directly enclosing class or enum declaration (§8.5).

(6)The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.

(7)A local class is a nested class (§8) that is not a member of any class and that has a name (§6.2§6.7).

public void test(){
		class A{
			class B{}
		}
}

按照定义,则类B应该不为local class。等待确认..... 

(1)嵌套静态类是任何一个在另外一个类或者接口的代码体中声明的类,它作为外嵌类或者接口的静态成员。嵌套静态类的行为类似于顶级类,唯一的差别是嵌套静态类前除了有 Java 包的名字以外还有外嵌类的名字。嵌套静态类是静态成员,因此没有 this 指针指向外嵌类的实例,也就是说嵌套静态类不能访问外嵌类的实例数据。当一个重要的数据结构紧密的关联与外嵌类,并且该数据结构有足够的内容保证它能够拥有一个类的时候,一般情况下我们可以使用嵌套静态类。
 
内部类有三种形式:成员类、本地类和匿名类,每个类依次都是对它前一个类的改进。
 
(2)内部类的可视范围是它的直接外嵌类(这一点和嵌套静态类不同,嵌套类在其外嵌类之外也是可视的),也就是说内部类可以直接引用外嵌类中的任何成员。因为内部类与外嵌类的实例相关,所以内部类拥有两个 this 指针,一个指向内部类的实例本身,一个指向外嵌类实例。内部类的一个主要用途是用于 GUI 的事件处理程序。
 
(3)本地类是在代码块中声明的,因此它不是外嵌类的成员。一般来说,本地类在方法中进行声明。成员类和本地类的差别很小,通过把本地类移到方法的外部,我们可以很容易得把它转换成为成员类。它们两个之间最重要的差别是,本地类只能访问 final 的变量或者参数。  
 
(4)匿名类允许我们把类的定义和实例的分配组合在一起。无须像其它任何嵌套类那样进行声明,我们可以直接用 new SomeClass() 实例化对象,并且在实例化的位置把整个类的实现都放在花括号中。
interface IOuter{
	int a = 2;  // only public, static & final are permitted
	class INestedClass{ // only public ,static are permitted

	}
}

public class TopLevelClass{
	class NestedClass{ // only static,public,protected,private,final are permitted

	}

	class InnerClass{ // only public,protected,private,final are permitted

	}

	public void test(){
		class LocalClass{ // only abstract or final is permitted

		}
	}
}

关于类继承内部类:

class WithInner {
	class Inner {	}
}

class InheritInner extends WithInner.Inner {
	// 第一种写法
	InheritInner(WithInner wi) {
		wi.super();
	}
	// 第二种写法
	InheritInner() {
		new WithInner().super();
	}

	public static void main(String[] args) {
		WithInner wi = new WithInner();
		InheritInner ii = new InheritInner(wi);
	}
}

  

这是在《JAVA编程思想》上看到的一道例题,是关于继承inner classes(内隐类)的。由于inner class的构造函数必须连接到一个reference指向outer class对象身上,所以当你继承inner class时,事情便稍微复杂些。问题出在“指向outer class对象”的那个神秘reference必须被初始化。但derived class之内不存在可连接的缺省对象,这个问题的答案是,使用专用语法,明确产生该关联性: 

参考:

(1)深入理解为什么Java中方法内定义的内部类可以访问方法中的局部变量

(2)https://blog.csdn.net/he_world/article/details/50252039

Javac之inner与nested类的更多相关文章

  1. Javac语法糖之Enum类

    枚举类在Javac中是被当作类来看待的. An enum type is implicitly final unless it contains at least one enum constant ...

  2. Java学习笔记5---命令行下用javac,java编译运行含package语句的类

    对于笔记3中的HelloWorld程序,编译时只要输入javac HelloWorld.java即可生成类文件:再用java HelloWorld即可运行. 如果源程序使用了包声明,编译运行时要使用某 ...

  3. Mac下 javac java 进行编译和运行含有包路径及引入jar包的类

    近两天因为刚入职,属于熟悉环境的阶段,研究了下算法(第四版),当不使用IDE工具直接使用终端进行javac 编译带有包的类,然后使用java 会出现如下错误提示: 使用谷歌搜索了很久,终于找到解决的办 ...

  4. Nested Class(嵌套类)

    在类.结构或接口中定义的类型称为嵌套类型. 例如 public class Container { class Nested { Nested() { } } } 不论外部类型是类.接口还是构造,嵌套 ...

  5. 【总结】java命令解析以及编译器,虚拟机如何定位类

    学Java有些日子了,一直都使用IDE来写程序.这样的好处就是能让我连如何用命令行编译,解释执行Java源代码都不知道,就更不清楚JDK中的编译器和虚拟机(包含字节码解释器)是如何定位到类文件的.悲哀 ...

  6. 利用ant的javac任务来编译java程序

    <?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...

  7. 关于javac和java

    1.为什么安装完jdk后不配置环境变量就能直接运行java,而不能运行javac 在安装jdk的时候jdk会自带一个jre(java运行环境),还会单独安装一个jre,默认路径是和jdk在同级目录,而 ...

  8. java包的所有类生成class

    javac的编译单位其实就是单个的java文件,为了达到同时编译多个java文件的目的,可以将所需编译的java文件路径保存在一个txt中,比如sourcelist.txt,以换行为分隔符(这个过程称 ...

  9. java中Runtime类和Process类的简单介绍

    在java.lang包当中定义了一个Runtime类,在java中对于Runtime类的定义如下: Java code public class Runtime extends Object 每个 J ...

随机推荐

  1. Spring全局异常处理

    最近学习Spring时,认识到Spring异常处理的强大.之前处理工程异常,代码中最常见的就是try-catch-finally,有时一个try,多个catch,覆盖了核心业务逻辑: 1 try{ 2 ...

  2. 简单配置vps,防ddos攻击

    防人之心不可无. 网上总有些无聊或者有意的人.不多说了.上干货,配置vps apf防小流量ddos攻击. 对于大流量的ddos攻击, 需要机房的硬件防火墙,vps内部可能也扛不住. 1. 安装 DDo ...

  3. asp.net 使用Oracle数据库

    asp.net下使用oracle会发生“未能加载文件或程序集‘Oracle.DataAccess’或它的某一个依赖项”的错误.这说明Oracle的驱动没有安装好,或者版本不对的错误. 1.检查Orac ...

  4. Jenkins Pipeline+sonar构建质量平台

    前提: Jenkins JDK 目录: 1.安装sonar插件:SonarQube Scanner for Jenkins 2.安装SonarQube 3.安装sonar-scanner ++++++ ...

  5. asp.net core部署到iis

    asp.net core项目部署到IIS稍微不同于之前,记录几个要点: 一.下载安装.NET Core 托管捆绑包,下载地址,这个链接或许不是最新的了,官方文档里面有这个链接,到下图所示位置即可找到. ...

  6. 配置sql server 允许远程连接

    如果要想远程连接数据库那么则需要在一个局域网中或一个路由器中才可以做到 接下来就是具体的操作检查sqlserver数据库是否允许远程连接 具体操作为 (1)打开数据库,用本地帐户登录,右击第一个选项, ...

  7. [C#学习笔记]C#中的decimal类型——《CLR via C#》

    System.Decimal是非常特殊的类型.在CLR中,Decimal类型不是基元类型.这就意味着CLR没有知道如何处理Decimal的IL指令. 在文档中查看Decimal类型,可以看到它提供了一 ...

  8. DataGridViewComboBoxColumn值无效解决方法

    值无效,可能是你下拉框选项,没有这样的值,而你却设置这个值. dataGridView1.Rows[i].Cells[1].Value = "选项一"; 解决方法就是在窗体的构造函 ...

  9. cesium随笔 — 隐藏三维场景下方版权信息

    上图中的版权信息相信很多人都想去掉,那么下面我将介绍一种简单粗暴的方法将其隐藏起来: .cesium-widget-credits { display: none!important; visibil ...

  10. 虚拟化 - Hyper-V

    不能和VMware.VirtualBox同时使用 网络 交换机其实就是指网卡,只不过是虚拟的 内部交换机 外部交换机