Javac之inner与nested类

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

注意:
(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。等待确认.....
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类的更多相关文章
- Javac语法糖之Enum类
枚举类在Javac中是被当作类来看待的. An enum type is implicitly final unless it contains at least one enum constant ...
- Java学习笔记5---命令行下用javac,java编译运行含package语句的类
对于笔记3中的HelloWorld程序,编译时只要输入javac HelloWorld.java即可生成类文件:再用java HelloWorld即可运行. 如果源程序使用了包声明,编译运行时要使用某 ...
- Mac下 javac java 进行编译和运行含有包路径及引入jar包的类
近两天因为刚入职,属于熟悉环境的阶段,研究了下算法(第四版),当不使用IDE工具直接使用终端进行javac 编译带有包的类,然后使用java 会出现如下错误提示: 使用谷歌搜索了很久,终于找到解决的办 ...
- Nested Class(嵌套类)
在类.结构或接口中定义的类型称为嵌套类型. 例如 public class Container { class Nested { Nested() { } } } 不论外部类型是类.接口还是构造,嵌套 ...
- 【总结】java命令解析以及编译器,虚拟机如何定位类
学Java有些日子了,一直都使用IDE来写程序.这样的好处就是能让我连如何用命令行编译,解释执行Java源代码都不知道,就更不清楚JDK中的编译器和虚拟机(包含字节码解释器)是如何定位到类文件的.悲哀 ...
- 利用ant的javac任务来编译java程序
<?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...
- 关于javac和java
1.为什么安装完jdk后不配置环境变量就能直接运行java,而不能运行javac 在安装jdk的时候jdk会自带一个jre(java运行环境),还会单独安装一个jre,默认路径是和jdk在同级目录,而 ...
- java包的所有类生成class
javac的编译单位其实就是单个的java文件,为了达到同时编译多个java文件的目的,可以将所需编译的java文件路径保存在一个txt中,比如sourcelist.txt,以换行为分隔符(这个过程称 ...
- java中Runtime类和Process类的简单介绍
在java.lang包当中定义了一个Runtime类,在java中对于Runtime类的定义如下: Java code public class Runtime extends Object 每个 J ...
随机推荐
- Git config 配置文件
一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一次:即使你升级了,他们也会绑定到你的环境中.你也可以在任何时刻通过运行命令来重新更改这些设置. Git有一个工具 ...
- jsp request 获取路径
这篇教程不错:http://zjutsoft.iteye.com/blog/1084260 自己试验如下: System.out.println("-----------------serv ...
- ServiceStack.Text json中序列化日期格式问题的解决
标记: ServiceStack.Text,json,序列化,日期 在使用ServiceStack.Text的序列化为json格式的时候,当属性为datetime的时候,返回的是一个new date( ...
- c# 中 $符号的用法
var names = new List<string> { "jason", "Ana", "Felipe" }; forea ...
- if、else if 、else及switch...case使用小记(C#)
有时候编程编的久了,如果不停下来认真思考一下,即便是一些最基础的知识点,也可能让自己懵圈.其实,说到底还是打基础的时候没打牢,或者说自以为是地认为自己懂了,然后在打基础的时候就懒得思考懒得看了,结果就 ...
- Nmap 扫描
最近在家里学习渗透,看到了nmap对服务器进行端口扫描,记录学习如下: Nmap支持非常多的扫描方式,包括TCP Syn ,TCP Connect,TCP ACK,TCP FIN/Xmas/NULL, ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- 护网杯圆满结束,还不满足?不如来看看大佬的WP扩展思路~
护网杯预选赛 WP转载自:https://qingchenldl.github.io/2018/10/13/%E6%8A%A4%E7%BD%91%E6%9D%AFWP-BitPwn/#more WEB ...
- 【awk】按小时切割日志
需求: 把日志按日志内容中的小时数做切割 {hostname=ali-beijing-msync-3512} 2017-05-17 23:17:52.694 [info] <0.27292.70 ...
- 【Quartz】一个小Demo
Quartz是一个优秀的框架,功能强大,用法简单,比如说下面这个例子. (1)引入Jar包 <!-- quartz 模块 --> <dependency> <groupI ...