www.webdeveloper.com
Results 1 to 5 of 5

Thread: addWindowListener(new Item(){adWLisBody}) - Syntax is weird... Can enyone help?

  1. #1
    Join Date
    Jul 2004
    Posts
    71

    addWindowListener(new Item(){adWLisBody}) - Syntax is weird... Can enyone help?

    Hi everyone! I am quite new to JAVA but I know most of the basics, especially when it comes to language syntax. I was reading some tutorial where I found the following function:

    PHP Code:
    class SomeClass{...

            
    dlframe.addWindowListener(new WindowAdapter() //<--This is the WEIRD part
            
    // Opens addWindowListener method
              
    public void windowClosing(WindowEvent e)    
              { 
    // Opens windowClosing method
                 
    System.exit(0);
               } 
    // Closes windowClosing method
             
    }); // Closes addWindowListener method

    }//closes class 
    Instead of this function written like the following:


    PHP Code:
    class SomeClass{...

            
    dlframe.addWindowListener(new WindowAdapter()) //<--This is how I thought it should be on the first place;
            
    // Opens addWindowListener method
              
    public void windowClosing(WindowEvent e)    
              { 
    // Opens windowClosing method
                 
    System.exit(0);
               } 
    // Closes windowClosing method
             
    }; // Closes addWindowListener method

    }//closes class 
    I couldn't find the following syntax in the language specification
    PHP Code:

    object
    .method(parameters {method body}); 
    Instead I only found syntax like this:
    PHP Code:

    object
    .method(parameters){method body}; 
    But the funniest thing is that the syntax above works just fine and doesn't issue any compiler error messages. Could anyone please unconfuse me... ? Thank you.

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Its a method of coding often called defining-on-the-fly. Its just a way of overriding methods from a particular class without resorting to subclassing (in this case, we are overriding the windowClosing() method in the WindowAdapter class).

    Its helpful when you only need to use one method from an Interface or abstract class.

    Regards.

  3. #3
    Join Date
    Jul 2004
    Posts
    71
    Thanks. Does it mean that the "WindowAdapter" object is first being created as a new object with its "windowClosing()" function overwritten and then this object is passed to the "addWindowListener()" as a parameter? If so, that's a pretty tricky syntax, expecially for beginners. I found this example in several "beginners" tutorials on Sun's website as well as some other places. Buntine, you have successfully "unconfused" me. Thank you.

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Yer, thats pretty much what is happening. It is a little bit tricky, but yo get used to it after you understand what is happening.

  5. #5
    Join Date
    Oct 2012
    Posts
    1
    OMG;; This forum was filled out in 2004;; But i'll ask for your help. I've known WindowsAdaptor class is an abstract class. so it can't be used with a 'new' operator. It makes me confused..anybody help me.. p.s. I'm poor at english. sorry..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles