File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Interthread communication program doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interthread communication program doubt" Watch "Interthread communication program doubt" New topic
Author

Interthread communication program doubt

Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19

Hey can anyone please tell me :
1. At line 5, which q are these?
2. Why is, at line 6, no object is instantiated? Directly the start function is called?
Thanks...




PS :
Then
calling
new Producer(q);
would be exactly the same as
Producer re = new Producer(q);
If so, then the 2nd way is useful for?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Welcome to the Ranch!

1. The first q (this.q) is the member variable declared in line 2. The second q is the argument variable from line 4. So, you're setting the member variable to the value of the argument variable.

2. An object is instantiated. Why do you think there isn't? (A variable is not the same as an object). There's no variable, instead the start() method is called directly on the new Thread object. This code does exactly the same as:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19

Thanks!


Then
calling
new Producer(q);
would be exactly the same as
Producer re = new Producer(q);
If so, then the 2nd way is useful for?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

The second approach is what you need to do if you want to keep a reference to the new object. With the first approach you create a new object, but you can't then call any methods on it later in the method.

For example:
or
Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19
Thanks
Matthew!
 
Don't get me started about those stupid light bulbs.
 
subject: Interthread communication program doubt