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 ...
随机推荐
- 第二届CCCC赛后感想 2017-04-15 23:56 88人阅读 评论(0) 收藏
第一次写赛后感想,也不算什么很正规的比赛,不过这次比赛的时间恰好处于思想变化的阶段,留贴纪念. 先谈谈这次比赛,弱校萌新,依靠申请进了总决赛,发现和第一届不一样,缺少了团队奖心中有点缺乏动力,比赛2个 ...
- XML--使用XML来将字符串分隔成行数据
DECLARE @xml XML SET @xml=CAST(REPLACE('<ROOT><X>'+'AA,AB,AC,AD'+'</X></ROOT> ...
- docker 操作命令详解
docker attach命令-登录一个已经在执行的容器 docker build 命令-建立一个新的image docker commit命令-提交一个新的image docker cp命令-将容器 ...
- 关于SqlServer连接错误
以前用数据库好好的,今天突然就出现连接错误,贴出出错误消息 出现这种错误的原因:服务里面sqlserver服务没有打开. 解决方案 : 计算机右键,打开管理,找到服务,把服务里面的SQL Server ...
- update sharepoint 2013 cu error
1. 安装过程合理: A. 可以同时在管理中心.两台前端.搜索服务器上安装重新发布的SP1补丁包(所提供的链接) B. 等待所有SP1补丁包安装完成,依次在管理中心.两台前端.搜索服务器上运行配置向导 ...
- Luogu4551 最长异或路径
题目链接:戳我 emmmmmmmmmm异或一个数两次等于没有操作对吧...所以我们按照前缀的异或和,建一个01trie.....然后之后.....直接在树上贪心地找能和它每一位不一样的数....然后. ...
- HTTP调用接口方法
1.创建接口调用方法类 package cn.com.victorysoft.sjzx.Message; import java.io.BufferedReader; import java.io.I ...
- GO学习笔记 - 函数名前面是否有输入参数肯定是不一样的!!
在刚接触GO语言时候,我相信你也会有这种困惑,为什么有的函数名前面有输入参数,而一些却没有,它们是否有差别?确实有差别,没有输入参数,是一般的函数:有输入参数,是结构的方法,输入参数叫做“方法接收者” ...
- MySQL大数据量的导入
最近在公司备份数据库数据,简单的看了一下.当然我用的是简单的手动备份. 第一:其实最好的方法是直接用: mysqldump -u用户名 -p密码 数据库名 < 数据库名.sql 在linux在操 ...
- BZOJ 3110 [Zjoi2013]K大数查询 (CDQ分治+树状数组)
题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是 ...