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

creating new methods in anonymous class should be an error

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.1.5
    • tools
    • generic, x86
    • generic, windows_95



      Name: tb29552 Date: 04/01/98


      If I misspell the name of a method I'm overriding
      in an anonymous class, I would expect the compiler
      to generate an error, the way it does for a
      misspelled variable name. For example, it took me
      hours to spot the reason this didn't work:

      theFrame.addWindowListener(new WindowAdapter()
      {
      public void WindowClosing(WindowEvent evt)
      {
      System.exit(0);
      }
      });

      Editor's note: The correct name is:
          "windowClosing(WindowEvent)." (Method in class java.awt.event.WindowAdapter)

      There is no way to call the WindowClosing method,
      so it shouldn't be allowed. Attempting to call it
      will generate a compiler error, so I shouldn't be
      allowed to do it at all.

      Granted, it is legal to write this:

      theFrame.addWindowListener(new WindowAdapter()
      {
      void someNewMethod()
      {
      System.exit(0);
      }

      public void windowClosing(WindowEvent evt)
      {
      someNewMethod();
      }
      });
      So can understand why the compiler allows new
      methods. But it's usually an error, so the
      compiler should be smart enough to detect that
      I've written a method that can't possibly be
      called, and generate an error.
      (Review ID: 27563)
      ======================================================================

            tturnidgsunw Todd Turnidge (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: