0

While reading the hibernate docs I've seen several times the use of static classes.

As an example here is an excerpt of the code in the documentation:

@Entity(name = "SystemUser")
public static class SystemUser {

    @EmbeddedId
    private PK pk;

    private String name;

    //Getters and setters are omitted for brevity
}

@Embeddable
public static class PK implements Serializable {

    private String subsystem;

    private String username;

    public PK(String subsystem, String username) {
        this.subsystem = subsystem;
        this.username = username;
    }

    private PK() {
    }

    @Override
    public boolean equals(Object o) {
        if ( this == o ) {
            return true;
        }
        if ( o == null || getClass() != o.getClass() ) {
            return false;
        }
        PK pk = (PK) o;
        return Objects.equals( subsystem, pk.subsystem ) &&
                Objects.equals( username, pk.username );
    }

    @Override
    public int hashCode() {
        return Objects.hash( subsystem, username );
    }
}

I thought top level static classes where not allowed in Java. What is the reason for indicating this classes as static?

wallek876
  • 3,219
  • 2
  • 19
  • 29
  • **"I thought top level static classes where not allowed in Java"** why ? – c0der Jun 02 '18 at 11:17
  • 1
    *The modifier static pertains only to member classes, not to top level or local or anonymous classes* - [JLS](https://docs.oracle.com/javase/specs/jls/se10/html/jls-8.html#jls-8.1.1). These static classes in docs should be a part of some enclosing class in this case. – Oleksandr Pyrohov Jun 02 '18 at 11:34
  • @c0der after reading about the topic is what I understand, like https://stackoverflow.com/questions/7486012/static-classes-in-java or as pointed out by Oleksandr – wallek876 Jun 02 '18 at 13:16
  • Sorry, I misunderstood the question. – c0der Jun 02 '18 at 13:50

0 Answers0