您的位置:首页 > 编程语言 > Java开发

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:

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.



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  effictive java