Monday, July 29

Item 22: Favor static member classes over nonstatic

             Favor static member classes over non static

What is static member class?

A nested class declared with static keyword

A non-static class has no static keyword with it .

What is major difference?

Non- static member class can't be referred standalone. It can be accessed with enclosing class only.
So when an instance of non-static class is created association with its enclosing class it created itself

And that cost extra time of instance initiation and also extra memory space for association. This association may not be required at all.

So one should always prefer static member class As that does not create any association with enclosing class.

So non-static member class should be created only when association with enclosing class is actually required i.e. there is some dependency on other part of the class as well along with member class Otherwise static member class is enough.

That saves not only time but also space So deliver great performance.

No comments:

Post a Comment