Java: Multiple class declarations in one file

In Java, you can define multiple top level classes in a single file, providing that at most one of these is public (see JLS §7.6). See below for example.

  1. Is there a tidy name for this technique (analogous to inner, nested, anonymous)?

  2. The JLS says the system may enforce the restriction that these secondary classes can’t be referred to by code in other compilation units of the package, e.g., they can’t be treated as package-private. Is that really something that changes between Java implementations?

e.g., PublicClass.java:

package com.example.multiple;

public class PublicClass {
    PrivateImpl impl = new PrivateImpl();
}

class PrivateImpl {
    int implementationData;
}

9 Answers
9

Leave a Comment