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
Joined: Dec 20, 2001
Posts: 3271
posted
0
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
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