views:

61

answers:

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?

+5  A: 

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.
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
Given that the constructor is already private, it does still seem redundant. Item 4 of Effective Java leaves out the final.
Yishai
+1  A: 

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.

Yishai