Using nested enum types in Java

Drink.COFFEE.getGroupName(); Drink.COFFEE.COLUMBIAN.getLabel(); First off, that sample code you gave violates the “law of demeter” somewhat – as the COLUMBIAN instance field is only used to retrieve the label. Also, with that structure, COLUMBIAN has to be an instance of the COFFEE enum, but I don’t think that’s what you’re really going for here. someMethod(Drink type) … Read more

How to restrict access to nested class member to enclosing class?

Actually there is a complete and simple solution to this problem that doesn’t involve modifying the client code or creating an interface. This solution is actually faster than the interface-based solution for most cases, and easier to code. public class Journal { private static Func<object, JournalEntry> _newJournalEntry; public class JournalEntry { static JournalEntry() { _newJournalEntry … Read more

Are inner classes in C++ automatically friends?

After having asked more or less the same question here myself, I wanted to share the (apparently) updated answer for C++11: Quoted from https://stackoverflow.com/a/14759027/1984137: standard $11.7.1 “A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to … Read more

Why Would I Ever Need to Use C# Nested Classes [duplicate]

A pattern that I particularly like is to combine nested classes with the factory pattern: public abstract class BankAccount { private BankAccount() {} // prevent third-party subclassing. private sealed class SavingsAccount : BankAccount { … } private sealed class ChequingAccount : BankAccount { … } public static BankAccount MakeSavingAccount() { … } public static BankAccount … Read more