Nested Classes(just for self-learning)
来源:互联网 发布:淘宝网店首页海报尺寸 编辑:程序博客网 时间:2024/06/04 18:17
this piece of writing is reprinted from the following url:
http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... }}
Nested classes are divided into two categories: static and non-static. Nested classes that are declaredstatic
are simply called static nested classes. Non-static nested classes are calledinner classes.
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass
, a nested class can be declaredprivate
, public
, protected
, or package private.(Recall that outer classes can only be declaredpublic
or package private.)
Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declaredprivate
. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Inner Classes
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
class OuterClass { ... class InnerClass { ... }}
An instance of InnerClass
can exist only within an instance of OuterClass
and has direct access to the methods and fields of its enclosing instance.The next figure illustrates this idea.
An Instance of InnerClass Exists Within an Instance of OuterClass
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes). Both of these will be discussed briefly in the next section.
Note: If you want more information on the taxonomy of the different kinds of classes in the Java programming language (which can be tricky to describe concisely, clearly, and correctly), you might want to read Joseph Darcy's blog:Nested, Inner, Member and Top-Level Classes.
- Nested Classes(just for self-learning)
- Nested classes
- nested classes
- Nested Classes
- Useful links for self-learning Eclipse debugger
- Nested Classes和Inner Classes
- Self-Supervised Learning for Stereo Matching with Self-Improving Ability
- Nested classes (C++ only)
- Nested Classes嵌套类
- java-basics [nested classes]
- Self-Learning
- Java中的Nested Classes和Inner Classes
- SeLeCT:Self-Learning Classifier for Internet Traffic
- 笔记:Self-Taught Low-Rank Coding for Visual Learning
- 《Deep Self-Taught Learning for Weakly Supervised Object Localization》
- 嵌套类(Nested Classes)
- Java - nested classes 嵌套类
- java_嵌套类(Nested Classes)
- Linux, Mac下Latex的IDE
- ZOJ 1082 Stockbroker Grapevine【Bellman-Ford】
- S7-1200编译: 程序中存在地址模糊的对象
- Programming Optimization
- iphone开发之剪贴板详解
- Nested Classes(just for self-learning)
- (转)D7 combobox csDropDownList 赋值
- 彻底接管程序异常处理
- linux下调试core的命令,察看堆栈状态命令
- base sdk missing解决办法
- Java报表条件属性概要
- 周末
- 诺基亚的问题、挑战、未来
- 如何改变浮躁心理