LocalDate hasn't constructor… How can we create classes without constructor?

  • A+

I'm reviewing Java 8 classes of the Time package and I have a doubt about the classes without constructor like LocalDate class.

If you create a Java class, this class will always have a default constructor, nevertheless LocalDate hasn't constructor, that is to say, you can't do this:

LocalDate date = new LocalDate();

If you do this, you will get a compile error "the constructor LocalDate() is undefined".

Why LocalDate hasn't a default constructor?

And the most important question... How can I create a class without constructor that only can be instanced calling static methods?

Thank you very much and regards.


The typical pattern here is a class with only private or package default constructors, combined with a factory method that is either a public static method of the class or a method of an accompanying factory class. You get LocalDate objects from many static methods listed in the javadoc.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: