Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4492260

Provide a "once" keyword for a once-only evaluation of designated methods

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 1.4.0
    • Fix Version/s: None
    • Component/s: specification
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description



      Name: ddT132432 Date: 08/15/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)

      The following coding idiom, and variations of it, occur extremely frequently
      in Java programs:

       ----------------------
       :
       private SomeType var;
       public SomeType getVar() {
         if (var == null) {
           var == new SomeType(...);
         }
         return var;
       }
       :
       ----------------------

      A "once" keyword would reduce program size and improve readability
      significantly. Example:

       ----------------------
       public once SomeType getVar() {
         return new SomeType(...); // Always return ref to same instance
       }
       ----------------------

      Note that this is *not* the same as a static method declaration, since different
      instances of class X which contains a non-static "once"-method with can deliver
      different values (note this is in contrast to Eiffel's "once", which returns the
      same value for all class instances.)

      "once" can also be used in static method declarations:

       ----------------------
       public static once void initializeSystem() {
         // Do initialization
         :
       }
       ----------------------

      This example also shows "once"'s use in a procedure. Currently the only way to
      code this is by using tedious and error-prone code such as:

       ----------------------
       private static boolean alreadyInitalized = false;
       public static synchronized void initializeSystem() {
         if (!alreadyInitalized) {
            // Do initialization
            :
            alreadyInitalized = true;
         }
       }
       ----------------------

      "once" methods are implicitly synchronized, since the "once" keyword garantees
      that exactly one instance of the object is instantiated.

      Primitive types work similarly, effectively caching the result in an unnamed
      private instance or (in the case of static methods) class variable. All other
      aspects of method semantics remain unchanged.

      Like "synchronized", the "once" declaration is *not* inherited, although
      calling a "once"-method from a subclass e.g. super.getVar(), continues to
      exhibit the once-only behaviour.

      Optimizing compilers and runtime environments can certainly utilize the "once"
      declaration to perform additional optimizations. For example, the method only
      need be "synchronized" until the first call to it has completed, and the monitor
      released.
      (Review ID: 130050)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                abuckley Alex Buckley
                Reporter:
                ddressersunw Daniel Dresser (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: