When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we need to add inner classes to normal one? Leave alone all the specific details of the Java syntax, I think the reason is quite SIMPLE: we are human beings and we are always looking for approaches with LESS effort to get things done. In a word, we are lazy and sometimes laziness can boost our technology to an unprecedented extend.

Access Control
Inner classes in Java have special access privileges from the scope in which they are defined. They can access all the fields of the outer class including date that would otherwise be private. So when it comes to the situation in which ONE class need to access some private data in another class and the private data are ONLY visible to this class, we'd better use inner class. Instead, we will need to define an access method of the outer class for the use of only one class. It is not worthwhile.

Then the question comes to HOW could the inner classes access fields of the outer class.
It's important to know that, the access control behavior is done by the compiler, not the virtual machine. In the virtual machine's perspective, the inner class and the outer one are two distinctive classes. With compilation, the inner class holds the reference of the outer one and the outer class adds special access methods implicitly. In the default constructor of the inner class, the reference of the outer class is passed to a filed added by the compiler in this way the inner class could know which instance it is bounded. Also, we can write the inner object constructor more explicitly, using the syntax:
outerObject.new InnerClass(construction parameters);

Local Inner Classes
The important thing that distinguishes local inner classes to normal ones is that, local inner classes are defined in the scope of a function. And local classes are never declared with access specifier (public/private) because they do not need one. They are only used in the scope of the function in which they are defined and they are totally hidden from the outer world, they are not visible even to other functions in the outer class.

There is another advantage of Java inner class: they can even access local variables which are defined final. The fact why the local variable should be defined final is that, the class gets the value in construction and if the value changed, it may cause some kind of conflicts.  If you really need to change the var value in the inner class, you can define the local var to be an array which means the reference to which can never be changed while the value it holds can be modified.

Anonymous Inner Classes
If we only need one instance of a class, we can use anonymous class which we do not need to specify a name for it. In Swing, we often create a new object of a class that implements the ActionListener interface.

Static Inner Classes
If we do not need the inner class hold the reference of the instance of the outer class, we use static classes. Only inner classes can be defined static.

Reference:
1. Core Java Vol 1. 8th edition. Page:258-295.

Java Inner Classes的更多相关文章

  1. Top 15 Java Utility Classes

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

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

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

  3. Summary of java stream classes

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

  4. Java - Nested Classes

    (本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...

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

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

  6. Java Date Classes

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

  7. Effective Java Chapter4 Classes and Interface

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

  8. [REPRINT] Java 101: Classes and objects in Java

    http://www.javaworld.com/article/2979739/learn-java/java-101-classes-and-objects-in-java.html?page=3 ...

  9. Replacing the deprecated Java JPEG classes for Java 7

    [src: https://blog.idrsolutions.com/2012/05/replacing-the-deprecated-java-jpeg-classes-for-java-7/] ...

随机推荐

  1. CobarClient源码分析(1)

    CobarClient是阿里巴巴公司开发一个的开源的.基于iBatis和Spring的分布式数据库访问层.为了支持iBatis,Spring框架提供了一个SqlMapClientTemplate,通过 ...

  2. Titanium系列--利用Titanium开发android App实战总结

    1. Titanium中,通过ImageView Controller 显示图片,图片地址要放在app/assets下,然后自己建一个文件夹,把图片放在这里.如下图,然后alloy代码:<Ima ...

  3. JSONP跨域数据调用

    引自:http://kb.cnblogs.com/page/139725/ Web页面上调用js文件时则不受是否跨域的影响(不仅如此,我们还发现凡是拥有”src”这个属性的标签都拥有跨域的能力,比如& ...

  4. day8---多线程socket 编程,tcp粘包处理

    复习下socket 编程的步骤: 服务端:   1 声明socket 实例 server = socket.socket()  #括号里不写  默认地址簇使用AF_INET  即 IPv4       ...

  5. 解决 yum安装时报错 Error: Protected multilib versions: 报错

    系统中缺少一个lib库 libz.so.1文件,使用yum安装会自动找到相关的rpm包,如下命令 # yum -y install libz.so.1 Resolving Dependencies-- ...

  6. 【原创】 SharePoint Service Unavaliable

    HTTP Error 503. The service is unavailable. 一般都是application pool停止工作了,停止原因可能是Identiy里的账号过期了什么的.

  7. C#函数式编程之缓存技术

    缓存技术 该节我们将分成两部分来讲解,第一部分为预计算,第二部分则为缓存.缓存这个技术对应从事开发的人员来说是非常熟悉的,从页面缓存到数据库缓存无处不在,而其最重要的特点就是在第一次查询后将数据缓存, ...

  8. MERGE 用法

    1.不带输出的SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[InsertShiGongJiao] ), @com ...

  9. MySql执行多条语句

    http://www.cppblog.com/fwxjj/archive/2010/08/05/122258.html说是在连接上设置: //链接时设定 mysql_real_connect( ... ...

  10. Linux:Vim

    模式介绍: Vim具备6种基本模式和5中派生模式. 普通模式 启动后的默认模式,用于:移动光标.删除文本等待,常用命令: dd:删除当前行. [number]dd:连续执行number对应次数的dd命 ...