mardi 16 mai 2017

Why to favor static member classes over nonstatic (Joshua Bloch Item #22)?

After reading that part of the book twice I still dont get why is this so broadly stated. Nonstatic member classes are part of the language. Agreed that not all parts of any programming language is perfet. Do the advatganes of nonstatic member classes, with their access to enclosingg instance's methods, so seldom outweight the possibly increased complexity of code (the tighter coupling between the inner and the outer class. BTW: accessing fields of outer class is also possible by static member classes, as long as the outer class is static as well)? No cons are really given in this part of the book.

What about non-static member classes for the Adapter pattern? Builder pattern?

What about non-static member classes for tiny objects (resembling e.g. C++'s struct) for keeping a few bits of data that belong together AND that belong to the outer class (so there is no reason to make them static) ?

(There seems to be a large excerpt on this item from Effective Java 2nd Edition by Joshua Bloch here: http://ift.tt/2rbpzDT - just in case someone does not have the book handy, to know what my starting point is).

Aucun commentaire:

Enregistrer un commentaire