Good question! Anonymous classes in Java allow you to implement adapter classes within your code. Huh? Pretty simple, if you have a class that returns an Enumeration interface (like a custom list, stack, etc.), you can define the implementation of the code right on the method declaration. To get a better grasp of it, check this example from the Java tutorial, also please note the last sentence regarding their usage in code.

Ok, how does this tie to .NET? Well, for some time I've been talking with Nick about this feature of the Java language and how sweet it will be to have it in C#. We'll get a taste of it in 2.0 with the addition of anonymous methods (particurlaly with delegates). But wouldn't it be cool to take advantage of this functionality, right now in 1.1? Well, you can if you use J#!

First, we need an interface...so, lets do it in C#:

namespace Lozanotek.Examples
{
    using System;

    public interface ICaller
    {
        void DoSomething();
    }
}

Next, we need a couple of classes to implement the interface...one in C# and the other in J#

C# Example

namespace Lozanotek.Examples
{
    using System;

    ///
    /// Summary description for CsImplementor.
    ///
    public class CsImplementor : ICaller
    {
        public CsImplementor() {}

        #region ICaller Members

        public void DoSomething()
        {
            Console.WriteLine("I'm implemented in {0}", GetType().Name);
        }

        #endregion
    }
}

J# Example

package Lozanotek.Examples;

import System.*;

/**
 * Summary description for JImplementor.
 */
public class JImplementor implements ICaller
{
    public JImplementor(){}

    public void DoSomething() {
        Console.WriteLine("I'm implemented in {0}", GetType().get_Name());
    }
}

Finally, we need a caller to call the implementing classes and have the ability to create an anonymous class...we have no other choice here but J#.

package Lozanotek.Examples;

import System.*;

/**
 * Summary description for Caller.
 */
public class JavaClient
{
    public JavaClient(){}

    /** @attribute System.STAThread() */
    public static void main(String[] args)
    {
        // Create the caller of the methods!
        CsClassCaller caller = new CsClassCaller();

        // Call the C# implementation
        caller.Execute(new CsImplementor());

        // Call the Java implementation
        caller.Execute(new JImplementor());

        // Define an implementation on the fly
        caller.Execute(new ICaller(){
            public void DoSomething() {
                Console.WriteLine("I'm an anonymous class being called from {0}", GetType().get_Name());
            }
        });

        Console.ReadLine();
    }
}

What does it output you ask? Here it is:

I'm implemented in CsImplementor
I'm implemented in JImplementor
I'm an anonymous class being called from 1

Pretty sweet, huh? If you guys want to the code let me know! Happy codin'!