Reading effictive Java:static member Class (SMC) and nonstatic member class (NSC)

Source: Internet
Author: User
Tags static class

Misunderstanding of static member class:

For the most programmers, comparing to SMC, we are more than familiar with static field. So we could analogously guess how SMC works according to static field ' s feature. That leads to a MISUNDERSTANDING.--SMC is only having one instance.

Prove misunderstanding is wrong:

We may see how stupid this is and after you think this. If It means client can ' t create an instance of a static class. The only-to-do constructor ' private ' or ' package default ', and don ' t provide any public method which has the Function of ' Create a instance '.

So the thing are if we use ' Static ' to modify a inner class, it doesn ' t has any matter with whether client can create Mul TI 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 PL Us:return a+b;case minus:return a-b;default://use 0 as default value,this might be inappropriate, just ignorereturn 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 D Efault Value,this might be inappropriate, just ignorereturn 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-is it, SMC was 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 the 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.