Skip to main content
gamedev.net
🔒 Locked

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

Started by fpsgamer Nov 13, 2008 at 7:15 PM 2 replies 1.3k views
Original Post
fpsgamer
fpsgamer
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?
larvyde
larvyde
You are new-ing an instance of an anonymous inner class of Main.main(String[]) which implements the interface Runnable and assigning it to doHelloWorld
Glass_Knife
Glass_Knife
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 methodsSomeInterface 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.
dmatter
dmatter
Quote:
Original post by fpsgamer
Are we creating some kind of lambda function or something?
It is similar to a lambda expression - it's the closest thing Java has. What you have there is an example of an Anonymous Inner Class, which is one of a few types of inner classes.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.