Sign in to follow this  

[java] Java Beginner: Please help eplain this code...

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I do not use Java very much. For that reason the following snippet has me confused:
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         Runnable doHelloWorld = new Runnable() {
             public void run() {
                 System.out.println("Hello World on " + Thread.currentThread());
             }
        };

        doHelloWorld.run();
    }
}

Are we creating some kind of lambda function or something? Are we deriving from Runnable? How? This syntax has me confused :( Could someone break down what is happening part-by-part?

Share this post


Link to post
Share on other sites
I'll give this a try...

Java uses interfaces for many reasons. Java doesn't support multiple inheritance, so interfaces allow for a class to be many types.

For example:

public class A extends B implements C, D, E { }



Java uses interfaces to allows objects that are not the same type to share the same functionality.

For example:

public interface Readable {
public int read();
}

public class MyFile implements Readable { }
public class MyList implements Readable { }
public class MySocket implements Readable { }



A file, a linked list, and a socket are not the same type, but you can read them.

Java also uses interfaces for callbacks.
For Example:

public MyClass {
public MyClass () {
JButton b = new JButton();
b.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
onButton(); // private method of MyClass
}
});
}

private void onButton() {

}
}



Here is an example of using an interface in two ways:

public class ClosureGameDev implements Runnable {

private String name = "Private Variable";

public ClosureGameDev() {
function( this );
function( new Runnable() {
public void run() {
System.out.println("I am anonymous " + name);
}
});
}

public void run() {
System.out.println("I am this " + name);
}

public void function( Runnable runnable ) {
runnable.run();
}

public static void main( String[] args ) {
new ClosureGameDev();
}

}


In this case, both runnable objects are almost the same. The difference is that one of them, the anonymous inner class, is created inside a function. The other one is a class that anyone could use. This is not exactly a lambda function because the variables that are local to that function can not be used unless they are constants.

If you have a lot of callbacks for GUI stuff, it would be a real pain in the butt to have your main class implement 6 different interfaces just to call a method on a button press or menu item.

Here is the mechanics:

// First we just make a new one, but after the constructor,
// "new SomeInterface()", we add the { } and define the methods
SomeInterface someInterface = new SomeInterface() {

// now we define any methods of the interface
public void fn() { }

// the class is done, so we close the class "}", and end the statement ";"
};

// if we have some method that takes an interface of this type, we can
// call it with our new anonymous inner class
someMethod( someInterface );

// If we want to be really lazy, we can skip defining the someInterface variable
// and just define the inner class inside the method call. This creates
// code that is very hard to understand for someone new to Java.

someMethod( new SomeInterface() {
public void fn() {
doSomething();
}
});



Hope that helps.

Share this post


Link to post
Share on other sites

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this