Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Style question on implementing interfaces

  1. #1
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Style question on implementing interfaces

    I'm studying up on Java multithreading and using the Runnable interface with Swing. Most examples look something like this:

    Java Code:
    SwingUtilities.invokeLater(new Runnable () {
        public void run () {
             // whatever you need to do 
        }    
    });
    Is this typical or do some people split out the interface code like this:

    Java Code:
    Runnable myRunnable = new Runnable () {
        public void run () {
             // whatever you need to do 
        }    
    };
    SwingUtilities.invokeLater(myRunnable);
    I'm just thinking that the second approach is a little easier on the eyes. It reduces indentation and visibly separates the logic, which can be a help, especially if it is non-trivial. For me, coming from a SQL Server background, it's a little like using CTEs instead of subqueries: the end result is the same but the visual separation makes it easier to read.

    Your thoughts?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Style question on implementing interfaces

    There's even a third way (and probably still more): you can define a class that implements Runnable and invokeLater(...) a new instance of that class.

    Use whichever style you're comfortable with; the first code snippet may make you a little cross-eyed, but it's something Swing programmers are used to seeing. Usually, the anonymous Runnable's run() implementation has just one line: it invokes a method of the containing class.

    In employment, these things are often mandated by the company's coding standards.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default Re: Style question on implementing interfaces

    Just wait until Java 8 (1.8?) is released for the public; those closures (lambdas) allow for even much shorter notations.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Question about standard code style
    By lenois in forum New To Java
    Replies: 9
    Last Post: 02-08-2013, 04:56 PM
  2. A question on Interfaces.
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-28-2012, 01:00 AM
  3. Question on interfaces
    By fatabass in forum New To Java
    Replies: 16
    Last Post: 01-23-2012, 03:33 PM
  4. Implementing more than one interfaces
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:50 AM

Posting Permissions

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