Java Inner Classes
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的更多相关文章
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...
- Java Nested Classes(内部类~第一篇英文技术文档翻译)
鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...
- Summary of java stream classes
Java’s stream classes are good for streaming sequences of bytes, but they’re not good for streaming ...
- Java - Nested Classes
(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...
- java 错误 classes路径配置错误
1. 错误显示页 2. 解决步骤 2.1. 查看 root cause 信息 org.springframework.beans.factory.BeanCreationException: Erro ...
- Java Date Classes
References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
- [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 ...
- 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/] ...
随机推荐
- 「2013-9-5」Configure WingIDE for better display of East Asian Glyphs
很久没写软件配置相关的博客了.这次对于 WingIDE 在 Windows 下的字体配置,折腾了好一阵子,略曲折,也反映了「不清楚原理和背景的情况下,盲人摸象的效率低下是必然」这条放之四海而皆准的赤果 ...
- 新公司入职第一天遇到的 关于 CSS 单行溢出文本显示省略号...的问题
上班第一天 前端岗位,因为公司这个项目是标准开发 所以没法用框架在打架页面,好吧 我手写 各种div 各种css样式 好不忧伤,好吧 不废话了 进入正题. 想在导航栏中的 客户信息 功能点 实现溢出用 ...
- 【基础知识】.Net基础加强07 天
一. 自定义泛型 1. 泛型的目的:提高代码的复用性. 2. T:一般叫做“类型参数”,把数据类型作为参数传递 .一般用T类表示或者以大写T开头的比如:TKey,TValue.TOutput.TRes ...
- ASP.NET Core 源码阅读笔记(5) ---Microsoft.AspNetCore.Routing路由
这篇随笔讲讲路由功能,主要内容在项目Microsoft.AspNetCore.Routing中,可以在GitHub上找到,Routing项目地址. 路由功能是大家都很熟悉的功能,使用起来也十分简单,从 ...
- C语言 队列 顺序结构 实现
一个能够自动扩容的顺序结构的队列 ArrQueue (GCC编译). /** * @brief C语言顺序结构队列的实现 * @author wid * @date 2013-10-30 * * @n ...
- SpringMVC文件上传实现
SpringMVC(注解)上传文件需要注意的几个地方:1.form的enctype="multipart/form-data",这个是上传文件必须的2.applicationCon ...
- spring配置entitymangerfactory
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ...
- T4模板TextTemplatingFileGenerator和TextTemplatingFilePreprocessor区别
最近做一个项目,需要根据数据库表生成对应的实体类,于是想到了代码生成器.查阅了Nvelocity.T4.RazorEngine,对于一个微软技术派,觉得还是T4最亲切,使用简单,功能强大. 在尝试使用 ...
- mac下mysql数据库的配置
这里记录一下. 之前在mac下使用brew install mysql安装,但是安装完成后发现密码不好修改,上网搜了下发现mac下使用命令行安装mysql确实存在很多问题,这一点确实远不如Ubuntu ...
- bzoj 1191: [HNOI2006]超级英雄Hero
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MB 二分图匹配... Description 现在电视台有一种节目叫做超 ...