Empty constructor in FeedReaderContract demo

Viewed 287

Referring to the FeedReaderContract class on Android Developers page:

The code starts as:

public final class FeedReaderContract {
    // To prevent someone from accidentally instantiating the contract class,
    // give it an empty constructor.
    public FeedReaderContract() {}

i.e. there is a default public constructor for the class.

  • Is that code comment correct?
  • How does a public empty constructor prevent instantiation - should this maybe be private?

... am I still half asleep ? ...


PS I'm not concerned about the rest of the example, or how to use it. I am purely trying to confirm that I'm not going mad and stupid at the same time...

2 Answers

Self answer because this may help beginners?

The code comment is wrong. Empty constructor works fine.

To prevent instantiation, the constructor needs to be private, not empty.

Related