"A little progress everyday adds up to big results"

Sunday 16 September 2012

Clear explanation of Java's Ugly Syntax

Introduction:
               Most of the Java programmers might have worked with the (Ugly) syntax of an anonymous class. This post will cleanly explain the Ugly syntax (especially with Anonymous classes) of Java. For example,  consider the button click event in Java Swing.

class EventHandler implements ActionListener
{
         public void actionPerformed(ActionEvent e)
         {
                     // Code that handles the event
         }
}

// Somewhere else
ActionListener e = new EventHandler();
button_object.addActionListener(e);


This is a very straight forward way, where each and every process is done step by step. Now, before moving on to the equivalent ugly syntax code, we must be knowing 5 Java concepts.

1. Interface object concept:
                                 An interface can have an object to which, an object of one of the classes implementing it, can be assigned. For example, in the above code, ActionListener is an interface and e is an object of it. We have instantiated e by invoking the new operator on the EventHandler class (which implements the ActionListener interface).

2. Anonymous object concept:
                                 The synonym for the term 'Anonymous' is 'nameless'. There might be some situations when we would require the object of a particular class, in just one statement. In such cases to avoid 2 lines of code, we may go for an Anonymous object. 

For example:

ActionListener e = new EventHandler();

button_object.addActionListener(e);


could be written as

button_object.addActionListener(new EventHandler());

                           This is done purely to reduce the lines of code; however doing so affects the readability of the code, whereas the efficiency of the code remains unaltered.

3. Method implementation specific to an object:
                                While writing multi-threading code in Java, one might have written code something like this.

Thread t = new Thread()
{
          public void run()
          {
                   // Thread code
          }
};


One can say that this code implements the method run(), which is specific to the object t.

4. Method implementation specific to an instance of an interface:
                          This is nothing but the combination of the concepts 1 and 3. This concept will be well understood with the following examples.

Runnable t = new Thread()
{
     public void run()
     {
          // Thread code

     }
};


Using this concept with the ActionListener interface,

ActionListener e = new EventHandler()
{
          public void actionPerformed(ActionEvent e)
         {
                     // Code that handles the event
         }
};


5. Anonymous class concept:
                              Bit tricky to understand, but would be simple with an example. The syntax of an anonymous class is
new <Implemented interface/Super class>()
{
           // Code inside the class
}

For example,
class A
{
       // block of A
}
interface B
{
       // block of B
}
class C extends A implements B
{
       // block of C
}
class Main
{
        B b = new B()
        {
               // methods of the interface B are implemented here
        };
}

                
Putting them all together:                        
                            Now, we are going to put them all together. So, the click event code (mentioned in the second paragraph) can be written as follows.

button_object.addActionListener ( new ActionListener()
{
        public void actionPerformed(ActionEvent e)
        {
            // Code that handles the event
        }
} );


                Well...! Hope you might have understood. If not, please don't worry, you will understand it when you use these concepts in your code.

Now tell me, isn't Java interesting and challenging ?

No comments:

Post a Comment