码迷,mamicode.com
首页 > 编程语言 > 详细

Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)

时间:2016-05-07 10:52:07      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

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:

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.



 


Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)

标签:

原文地址:http://blog.csdn.net/surpassno/article/details/51331474

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!