Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)
2016-05-06 18:16
435 查看
Misunderstanding of static member class :
For most programmers, comparing to SMC ,we may be more familiar with static field . so we may analogously guess how SMC works according to static field's feature. That leads to a misunderstanding.--SMC may only have one
instance.
Prove misunderstanding is wrong:
We may see how stupid this is , after you think this over. If it means client can't create an instance of a static class .The only way is to make all constructor 'private' or 'package default',and don't provide any public
method which have the function of 'create a instance'.
So the thing is if we use 'Static' to modify a inner class , it doesn't have any matter with whether client can create multi instances.
Application of SMC
1. SMC can work independently , you can move a SMC out of its enclosing Class. It works well.
e.g. here we try to do plus operation use Calculator:
if we move SMC out of enclosing Class:
result:
Calculator operation:PLUS result: 4
OuterOperator operation:PLUS result: 4
so it works well , so why bother to make class Operation a static member class. The answer is we can easily tell
Operation is a component of Calculator. So here is it, SMC are often used as an component of enclosing class.
another example is Node in HashMap(jdk1.8).
Differences between static member class (SMC) and nonstatic member class(NSC)
SMC doesn't need an instance of enclosing class to bind,while NSC does.
SMC
NSC
methods in Entry(getKey,getValue,setValue) don't need to refer to Map.
For most programmers, comparing to SMC ,we may be more familiar with static field . so we may analogously guess how SMC works according to static field's feature. That leads to a misunderstanding.--SMC may only have one
instance.
Prove misunderstanding is wrong:
We may see how stupid this is , after you think this over. If it means client can't create an instance of a static class .The only way is to make all constructor 'private' or 'package default',and don't provide any public
method which have the function of 'create a instance'.
So the thing is if we use 'Static' to modify a inner class , it doesn't have any matter with whether client can create multi instances.
Application of SMC
1. SMC can work independently , you can move a SMC out of its enclosing Class. It works well.
e.g. here we try to do plus operation use Calculator:
if we move SMC out of enclosing Class:
public enum OuterOperator { PLUS,MINUS }
public class Calculator { static enum Operation{ PLUS,MINUS } public int operate(int a, int b,Operation o){ switch(o){ case PLUS: return a+b; case MINUS: return a-b; default: //use 0 as default value,this might be inappropriate ,just ignore return 0; } } public int operate(int a, int b,OuterOperator o){ switch(o){ case PLUS: return a+b; case MINUS: return a-b; default: //use 0 as default value,this might be inappropriate ,just ignore return 0; } } public static void main(String[] args){ Calculator cal=new Calculator(); int result=cal.operate(3, 1, Calculator.Operation.PLUS); System.out.println("Calculator operation:"+Calculator.Operation.PLUS+" result: "+result); int result2=cal.operate(3, 1, OuterOperator.PLUS); System.out.println("OuterOperator operation:"+OuterOperator.PLUS+" result: "+result2); } }
result:
Calculator operation:PLUS result: 4
OuterOperator operation:PLUS result: 4
so it works well , so why bother to make class Operation a static member class. The answer is we can easily tell
Operation is a component of Calculator. So here is it, SMC are often used as an component of enclosing class.
another example is Node in HashMap(jdk1.8).
Differences between static member class (SMC) and nonstatic member class(NSC)
SMC doesn't need an instance of enclosing class to bind,while NSC does.
SMC
public class outClass { public static class innerClass{ public innerClass(){ System.out.println("innerClass"); } } public static void main(String[] args) { //outClass a = new outClass(); innerClass b = new innerClass(); innerClass c = new innerClass(); } }
NSC
public class outClass { public class innerClass{ public innerClass(){ System.out.println("innerClass"); } } public static void main(String[] args) { outClass a = new outClass(); innerClass b = a.new innerClass(); innerClass c = a.new innerClass(); } }So comparing to SMC, NMC may spend one more reference to refer to its enclosing instance. it waste space and time. but for using SMC, InnerClass need to meet the condition -- InnerClass's methods don't need enclosing instance's reference. like
methods in Entry(getKey,getValue,setValue) don't need to refer to Map.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树