Nested Class

简介: 1. 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 en

1. 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.

2. Taxonomy of Classes

2.1 Static Nested Classes

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.

 

2.1.1 Create an object for the static nested class

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
2.1.2 When to use static nested class
B is A's assistant class and only be used by A, define B as A's static nested class like JDK's LinkedList and it's Entry.

2.2 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.

 

2.2.1 Instantiate an inner class

 

OuterClass.InnerClass innerObject = outerObject.new InnerClass();
2.2.2 Obtain outer class's reference
OuterClass.this
2.2.3 When to use
Inner class's most important feature is it can access outer class's any fields or methods, for example, in JDK's Collection
library, every collection must provide a according Iterator to support unified collection traversal.
2.3 Anonymous Inner Classes
 An inner class within the body of a method without naming it

 

2.3.1 When to use

 

The class has few code to implement

Taxonomy of Classes in Java Programming

目录
相关文章
|
5月前
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Con
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Con
69 1
|
7月前
|
Java Spring
【已解决】Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception .lang.NullPointerEx
【已解决】Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception .lang.NullPointerEx
97 0
|
8月前
|
Java Maven Spring
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
1830 0
|
8月前
|
XML Java 数据库连接
mybatis报错:java.lang.IllegalArgumentException: Mapped Statements collection does not contain
mybatis报错:java.lang.IllegalArgumentException: Mapped Statements collection does not contain
|
Java 数据库连接 数据库
Unable to evaluate the expression Method threw ‘org.hibernate.LazyInitializationException‘ exceptio
Unable to evaluate the expression Method threw ‘org.hibernate.LazyInitializationException‘ exceptio
|
Java 关系型数据库 MySQL
15. 成功解决:java: Can't generate mapping method with primitive return type.
今天启动 SpringBoot 项目时,报了如下错误:`java: Can't generate mapping method with primitive return type.`
1068 0
|
Java 应用服务中间件 Maven
nested exception is java.lang.IncompatibleClassChangeError: Implementing class(已解决)
nested exception is java.lang.IncompatibleClassChangeError: Implementing class(已解决)
464 0
nested exception is java.lang.IncompatibleClassChangeError: Implementing class(已解决)
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exception is org.springframewor
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exception is org.springframewor
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exception is org.springframewor
【Junit 报错】Test class should have exactly one public zero-argument constructor和Test class can only have one constructor
错误1: 1 java.lang.Exception: Test class should have exactly one public zero-argument constructor 2 at org.
6172 0
Nested
Nested
92 0

热门文章

最新文章