Inner Classes


  • It allows you to group classes that logically belong together and to control the visibility of one within the other.
  • It knows about and can communicate with the surrounding class.

Creating inner classes

  • You place the class definition inside a surrounding class.
  • More typically, an outer class will have a method that returns a reference to an inner class.
  • If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName.

The link to the outer class

  • When you create an inner class, an object of that inner class has a link to the enclosing object that made it, and so it can access the members of that enclosing object.
  • Inner classes have access rights to all the elements in the enclosing class.
  • The inner class can access methods and fields from the enclosing class as if it owned them.
  • The inner class secretly captures a reference to the particular object of the enclosing class that was responsible for creating it.
  • When you refer to a member of the enclosing class, that reference is used to select that member.
  • An object of an inner class can be created only in association with an object of the enclosing class when the inner class is non-static.

Using .this and .new

  • If you need to produce the reference to the outer-class object, you name the outer class followed by a dot and this.
  • Sometimes you want to tell some other object to create an object of one of its inner classes.
  • To do this you must provide a reference to the other outer-class object in the new expression, using the .new syntax.
  • To create an object of the inner class directly, you must use an object of the outer class to make an object of the inner class.
  • It’s not possible to create an object of the inner class unless you already have an object of the outer class. because the object of the inner class is quietly connected to the object of the outer class that it was made from.
  • If you make a nested class (a static inner class), then it doesn’t need a reference to the outer-class object.

Inner classes and upcasting

  • Normal (non-inner) classes cannot be made private or protected; they may only be given public or package access.
  • You can’t even downcast to a private inner class (or a protected inner class unless you’re an inheritor), because you can’t access the name.
  • The private inner class provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation.

Inner classes in methods and scopes

  • Inner classes can be created within a method or even an arbitrary scope.

Anonymous inner classes

  • What this strange syntax means is "Create an object of an anonymous class that’s inherited from Contents."
  • If your base class needs a constructor with an argument, you simply pass the appropriate argument to the base-class constructor.
  • The semicolon at the end of the anonymous inner class doesn’t mark the end of the class body. Instead, it marks the end of the expression that happens to contain the anonymous class.
  • If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, the compiler requires that the argument reference be final.
  • With instance initialization, you can, in effect, create a constructor for an anonymous inner class.
  • In effect, an instance initializer is the constructor for an anonymous inner class. you can have only one of these constructors.
  • Anonymous inner classes can either extend a class or implement an interface, but not both. And if you do implement an interface, you can only implement one.

Factory Method revisited

  • Prefer classes to interfaces. If your design demands an interface, you’ll know it. Otherwise, don’t put it in until you are forced to.

Nested classes

  • You don’t need an outer-class object in order to create an object of a nested class.
  • You can’t access a non-static outer-class object from an object of a nested class.
  • Fields and methods in ordinary inner classes can only be at the outer level of a class, so ordinary inner classes cannot have static data, static fields, or nested classes.
  • A nested class does not have a special this reference, which makes it analogous to a static method.

Classes inside interfaces

  • A nested class can be part of an interface, any class you put inside an interface is automatically public and static.
  • You can even implement the surrounding interface in the inner class.
  • It’s convenient to nest a class inside an interface when you want to create some common code to be used with all different implementations of that interface.
  • You can use a nested class to hold your test code.

Reaching outward from a multiply nested class

  • It doesn’t matter how deeply an inner class may be nested—it can transparently access all of the members of all the classes it is nested within.

Why inner classes?

  • Typically, the inner class inherits from a class or implements an interface, and the code in the inner class manipulates the outer-class object that it was created within.
  • You could say that an inner class provides a kind of window into the outer class.
  • What is it that distinguishes an inner class implementing an interface from an outer class implementing the same interface? The answer is that you can’t always have the convenience of interfaces—sometimes you’re working with implementations.
  • Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.
  • One way to look at the inner class is as the rest of the solution of the multiple-inheritance problem.
  • But inner classes effectively allow "multiple implementation inheritance." That is, inner classes effectively allow you to inherit from more than one non-interface.
  • You’ll ordinarily have some kind of guidance from the nature of the problem about whether to use a single class or an inner class.
  • If you have abstract or concrete classes instead of interfaces, you are suddenly limited to using inner classes if your class must somehow implement both of the others.

Closures & callbacks

  • A closure is a callable object that retains information from the scope in which it was created.
  • An inner class is an object-oriented closure, it has permission to manipulate all the members, even private ones.
  • With a callback, some other object is given a piece of information that allows it to call back into the originating object at some later point.
  • If a callback is implemented using a pointer, however, you must rely on the programmer to behave properly and not misuse the pointer.
  • The closure provided by the inner class is a good solution—more flexible and far safer than a pointer.
  • When you create an inner class, you do not add to or modify the interface of the outer class.
  • The value of the callback is in its flexibility; you can dynamically decide what methods will be called at run time.

Inner classes & control frameworks

  • To apply an application framework, you typically inherit from one or more classes and override some of the methods.
  • A common problem in application programming is the graphical user interface (GUI), which is almost entirely event-driven.
  • Inner classes allow you to have multiple derived versions of the same base class, Event, within a single class.
  • Notice how inner classes almost look like multiple inheritance.
  • It’s more flexible to read the events from a file instead of hardcoding them.
  • You’ll see how elegantly inner classes are used to describe the actions of a graphical user interface.

Inheriting from inner classes

  • The problem is that the "secret" reference to the enclosing class object must be initialized, and yet in the derived class there’s no longer a default object to attach to.
  • You must use a special syntax to make the association explicit.

Can inner classes be overridden?

  • "overriding" an inner class as if it were another method of the outer class doesn’t really do anything.
  • The two inner classes are completely separate entities, each in its own namespace.

Local inner classes

  • A local inner class cannot have an access specifier because it isn’t part of the outer class, but it does have access to the final variables in the current code block and all the members of the enclosing class.
  • Local inner class can have a constructor, Anonymous inner class cannot have a named constructor, only an instance initializer.
  • Since the name of the local inner class is not accessible outside the method, the only justification for using a local inner class instead of an anonymous inner class is if you need a named constructor and/or an overloaded constructor, since an anonymous inner class can only use instance initialization.

Inner-class identifiers

  • Every class produces a .class file that holds all the information about how to create objects of this type (this information produces a "meta-class" called the Class object).
  • If inner classes are anonymous, the compiler simply starts generating numbers as inner-class identifiers.

Thinking in Java——笔记(10)的更多相关文章

  1. java笔记10之循环

    关于for    循环语句:for循环,while循环,do...while循环.        for循环格式:        for(初始化语句;判断条件语句;控制条件语句) {          ...

  2. Java笔记10:Struts2简单Demo

    1 下载struts-2.3.24.1-all.zip并解压缩,位置任意,比如我的位置是D:\Download\Java\struts-2.3.24.1 解压缩D:\Download\Java\str ...

  3. JAVA自学笔记10

    JAVA自学笔记10 1.形式参数与返回值 1)类名作为形式参数(基本类型.引用类型) 作形参必须是类的对象 2)抽象类名作形参 需要该抽象类的子类对象,通过多态实现 3)接口名为形参 需要的是该接口 ...

  4. Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息

    </pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...

  5. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  6. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  7. Hadoop学习笔记(10) ——搭建源码学习环境

    Hadoop学习笔记(10) ——搭建源码学习环境 上一章中,我们对整个hadoop的目录及源码目录有了一个初步的了解,接下来计划深入学习一下这头神象作品了.但是看代码用什么,难不成gedit?,单步 ...

  8. Java复习10.Servlet编程

    Java复习10. Servlet编程知识 20131008 前言: 之前在大三下的时候,学习了一个月的JSP和Servlet知识,但是没有什么项目经验,把JSP Web开发学习实录看了前面几张,后面 ...

  9. Java笔记 —— 继承

    Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...

随机推荐

  1. 【面经】【转】C++类型转换

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:type b = (type) a. C++风格的类型转换提供了4种类型转换操作符来应对不同场景的应用. const_cas ...

  2. Android一体式(沉浸式)状态栏的实现

    注:公司开发任务适配是在4.4版本之上进行,所以此适配仅在4.4之上进行测试. 1.主要使用了第三方的开源项目SystemBarTint,github:https://github.com/jgilf ...

  3. docker不稳定 short running containers with -rm failed to destroy

    正常运行以下命令 sudo docker run --rm busybox echo helloworld /var/log/upstart/docker.log 日志如下: // :: POST / ...

  4. POJ2135 Farm Tour(最小费用最大流)

    题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...

  5. MongoDB 入门之基础 DCL

    此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...

  6. linqPad快速学习LINQ(含视频)

    在这里我向大家推荐的一个具是LinqPad有了这个工具并熟练使用就可以很快学习并掌握linq 安装步骤: 使用LINQPad可以很方便的调试linq以及lambda表达式.其中自带了linq以及F#简 ...

  7. AdaBoost 和 Real Adaboost 总结

    AdaBoost 和 Real Adaboost 总结 AdaBoost Real AdaBoost AdaBoost AdaBoost, Adaptive Boosting(自适应增强), 是一种集 ...

  8. 使用linux命令行配置无线网链接

    1. 需安装wpa_supplicant https://wiki.archlinux.org/index.php/WPA_Supplicant_%28%E7%AE%80%E4%BD%93%E4%B8 ...

  9. 微软曝光眼球追踪新专利,未来或将可以使用眼球控制HoloLens

    想要在增强现实(AR)和虚拟现实(VR)中获得感官能力,计算机的配合非常重要.如何能够更好的追踪我们所感知的,计算机判断用户目光所向的能力就必须进一步提高.根据微软新曝光的专利,可能已经有一个解决方案 ...

  10. UVa 11324 & 强联通分量+DP

    题意: 一张无向图,求点集使其中任意两点可到达. SOL: 强联通分量中的点要么不选要么全都选,然后缩点DAG+DP 记录一下思路,不想写了...代码满天飞.