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

Nested/Inner class in external file

You can make the inner class package private which means that it will only be accessible from other classes in exactly the same package. This is also done quite frequently for hidden classes inside the standard JDK packages like java.lang or java.util. in pkg/MyClass.java public class MyClass { … } in pkg/MyHiddenClass.java class MyHiddenClass { … Read more

Eclipse warning about synthetic accessor for private static nested classes in Java?

You can get rid of the warning as follows: package com.example.bugs; public class WeirdInnerClassJavaWarning { private static class InnerClass { protected InnerClass() {} // This constructor makes the warning go away public void doSomething() {} } final private InnerClass anInstance; { this.anInstance = new InnerClass(); this.anInstance.doSomething(); } } As others have said, Eclipse is complaining … 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