Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question: ePractice Exam

 
sukhy johal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've been going through the Sun ePractice exams and had one question.
I don't understand why the following code would work without extending the Thread class - any ideas?
Thanks
1. public class Snow {
2. public static void main(String [] args) {
3. Snow s = new Snow();
4. new Thread() {
5. public void run() {
6. for (int w = 0; w < 2; w++) {
7. System.out.print(w);
8. }
9. }
10. }.start();
11. new Thread() {
12. public void run() {
13. for (int z = 5; z < 7; z++) {
14. System.out.print(z);
15. }
16. }
17. }.start();
18. }
19. }
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sukiuk,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sukiuk:
I don't understand why the following code would work without extending the Thread class - any ideas?

There is a special syntax being used here. Look at the following code:

This code creates an anonymous class that does extend Thread. That is accomplished by stating "new Thread() { ... }". You can find more about this in the JLS, §15.9.5 Anonymous Class Declarations.
I hope that helps,
Corey
 
sukhy johal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Corey - I "get" it now.
Sukhy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic