2

According to the App Engine docs, the PersistenceManagerFactory should only be created once in the application.

It provides this sample:

package guestbook;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private PMF() {}

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
} 

Why does PMF.java have to be a "public final class" in addition to making the pmfInstance a "private static final" object?

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
oms
  • 35
  • 4

2 Answers2

6

Classes should be final unless there's a good reason for them not to be.

There is no use case in which one would want to inherit from the PMF, so it should be final.

Anon.
  • 58,739
  • 8
  • 81
  • 86
  • I agree, though this is bound to cause debate. You might want to cite the item in _Effective Java_ that agrees with you. :-) – Laurence Gonsalves Feb 24 '10 at 20:31
  • Given that the constructor is already private, it does still seem redundant. Item 4 of Effective Java leaves out the final. – Yishai Feb 24 '10 at 20:36
1

PMF is a class that should not be instantiated, since it has no instance state or methods, it is strictly there to provide static methods and global state.

Item 4 in Effective Java provides this idiom, however it does not add that the class should be made final, as it would be impossible to subclass it anyway with a private constructor. And there it is explicitly recommended that the private constructor be documented to avoid exactly the confusion you are having.

In addition, this code sample is providing the Static Initialization workaround for double check locking.

Community
  • 1
  • 1
Yishai
  • 90,445
  • 31
  • 189
  • 263