473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

problem with java syntax

65
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)


Expand|Select|Wrap|Line Numbers
  1. public class Joining {
  2.     static Thread createThread(final int i, final Thread t1) {
  3.         Thread t2 = new Thread() {        // line   3
  4.  
  5.             public void run() {
  6.                 System.out.println(i+1);
  7.                 try {                       t1.join();   } 
  8.                 catch (InterruptedException e) {        }
  9.                 System.out.println(i+2);
  10.             }
  11.  
  12.         };                 // line 12
  13.         System.out.println(i+3);
  14.         t2.start();
  15.         System.out.println(i+4);
  16.         return t2;
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         createThread(10, createThread(20, Thread.currentThread()));
  21.     }
  22. }
Oct 2 '07 #1
2 1080
JosAH
11,448 Expert 8TB
Yes that syntax is terrible; you can extend a class or interface on the fly by
doing this:

Expand|Select|Wrap|Line Numbers
  1. T instance= new T() { /* extending stuff here */ }
  2.  
Where T is the base class or interface. It's quite handy to have but the syntax
is terrible, I agree.

kind regards,

Jos
Oct 2 '07 #2
r035198x
13,262 8TB
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)


Expand|Select|Wrap|Line Numbers
  1. public class Joining {
  2.     static Thread createThread(final int i, final Thread t1) {
  3.         Thread t2 = new Thread() {        // line   3
  4.  
  5.             public void run() {
  6.                 System.out.println(i+1);
  7.                 try {                       t1.join();   } 
  8.                 catch (InterruptedException e) {        }
  9.                 System.out.println(i+2);
  10.             }
  11.  
  12.         };                 // line 12
  13.         System.out.println(i+3);
  14.         t2.start();
  15.         System.out.println(i+4);
  16.         return t2;
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         createThread(10, createThread(20, Thread.currentThread()));
  21.     }
  22. }
Read about anonymous inner classes
Oct 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
10
by: Jonathan Ames | last post by:
Moving to C++ from Java, I'm still confused by some aspects of memory cleanup operations. For example, let's say I have a class MovingObject which maintains a pointer to another class...
1
by: iksrazal | last post by:
Hi all, I've been struggling to make this command work from Java: /usr/bin/mysql c4 --user=root --password=mypass -e "source /home/crissilva/c4.sql" Works fine as shown when run from the...
24
by: cs_hart | last post by:
I have an application in Java that I would like to port to c++ to integrate with existing c++ app. Is anyone aware of any tools to help? I found microsft has a java->c# convert, but the java app...
6
by: John Bailo | last post by:
http://www.informationweek.com/software/showArticle.jhtml?articleID=196600515 Developers Embrace Java, Drop Visual Basic "Developers have abandoned Microsoft's Visual Basic in droves during...
8
by: jesmi | last post by:
my code is: addRecord.cfm <cfinclude template="head.cfm"> <p> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td> <div align="center">
3
by: sumuka | last post by:
Hello, I'm doing a project in java using swings ,im able to create a panel and frame but i have a problem in event handling.I have added the MouseMotionListener but it's giving some error .Here i...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
1
by: prasath03 | last post by:
Hi, I am struggling with the following problem. Can somebody pls help me?. I am developing a wesbite for user can edit their files only, so i am going to develop a page for a Tree Structure in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.