problem with java syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    problem with java syntax

    i didnt understand the syntax in the code below at line 3.
    there is an object creation of type Thread but the stament dont have any
    terminating semi-colon, instead there is a block opening.
    and the block ends at line 12.
    what kind of syntax is this? is there anybody to explain me?
    (i guess it that this is not an advance topic, it is likely to be related with basics of java, but i could not figure out)


    Code:
    public class Joining {
        static Thread createThread(final int i, final Thread t1) {
            Thread t2 = new Thread() {        // line   3
            
                public void run() {
                    System.out.println(i+1);
                    try {                       t1.join();   } 
                    catch (InterruptedException e) {        }
                    System.out.println(i+2);
                }
                
            };                 // line 12
            System.out.println(i+3);
            t2.start();
            System.out.println(i+4);
            return t2;
        }
    
        public static void main(String[] args) {
            createThread(10, createThread(20, Thread.currentThread()));
        }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Yes that syntax is terrible; you can extend a class or interface on the fly by
    doing this:

    [code=java]
    T instance= new T() { /* extending stuff here */ }
    [/code]

    Where T is the base class or interface. It's quite handy to have but the syntax
    is terrible, I agree.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by stmfc
      i didnt understand the syntax in the code below at line 3.
      there is an object creation of type Thread but the stament dont have any
      terminating semi-colon, instead there is a block opening.
      and the block ends at line 12.
      what kind of syntax is this? is there anybody to explain me?
      (i guess it that this is not an advance topic, it is likely to be related with basics of java, but i could not figure out)


      Code:
      public class Joining {
          static Thread createThread(final int i, final Thread t1) {
              Thread t2 = new Thread() {        // line   3
              
                  public void run() {
                      System.out.println(i+1);
                      try {                       t1.join();   } 
                      catch (InterruptedException e) {        }
                      System.out.println(i+2);
                  }
                  
              };                 // line 12
              System.out.println(i+3);
              t2.start();
              System.out.println(i+4);
              return t2;
          }
      
          public static void main(String[] args) {
              createThread(10, createThread(20, Thread.currentThread()));
          }
      }
      Read about anonymous inner classes

      Comment

      Working...