10 Reasons We Need Java 3.0
Subject:   get rid of public constructors
Date:   2002-08-06 16:28:49
From:   friendless
I would like to see constructors all be private, so that you have to invoke a static factory method to get a new instance of anything. The problem with public constructors is that as soon as you call "new", memory is allocated, even if an identical object already exists. For immutable classes, such as Integer and String, arbitrary amounts of memory can be wasted by useless creation of duplicate instances. By making constructors private, use of memory by a class is controlled by the implementors of that class, who understand it best in any case.
Full Threads Newest First

Showing messages 1 through 2 of 2.

  • get rid of public constructors
    2002-08-07 01:03:15  jdavi [View]

    I agree with you in having a way for control if a constructor returns a new object or a reference to an existing one, but I would prefer an equivalent to the readResolve() in serialization or be able to assign the "this" variable to another instance inside the constructor.
  • get rid of public constructors
    2004-01-07 21:00:24  anonymous2 [View]

    Thats where design patterns come into play as the solution you suggested would not work in all cases.