• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interthread communication program doubt

 
Abhay Saini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Abhay Saini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
Matthew!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic