aspose file tools*
The moose likes Beginning Java and the fly likes isAlive() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "isAlive()" Watch "isAlive()" New topic
Author

isAlive()

Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Hello,

I don't understand the usage of "ob1.t.isAlive()" in the code below. I just don't understand that construct. Please help me understand how "ob1.t.method" works.



I know what the isAlive() method does, but just confused about the usage "ob1.t" to call it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Prasanna Raman wrote:Hello,

I don't understand the usage of "ob1.t.isAlive()" in the code below. I just don't understand that construct. Please help me understand how "ob1.t.method" works.



I know what the isAlive() method does, but just confused about the usage "ob1.t" to call it.


The ob1 portion is a local variable, which is a reference to a New class type. The New class object has an instance variable, named t, which is a reference to a Thread class type. So, to access this Thread object, you need to use "ob1.t".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Sorry, I am still not getting it. Could you please explain differently? I am trying to think if there may be other cases where we use this construct? Generally we call any method of a class using an object of the class.. is t an object of thread here? Then I understand t.isAlive() but how does ob1 come fit in here?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You should never use that construct at all. You should declare the field t with private access. You should gain access via a getT() method.
You have a reference called ob1 which has an accessible field called t and that t has a method called isAlive().
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Prasanna Raman wrote:Sorry, I am still not getting it. Could you please explain differently? I am trying to think if there may be other cases where we use this construct? Generally we call any method of a class using an object of the class.. is t an object of thread here? Then I understand t.isAlive() but how does ob1 come fit in here?


Well, simply, because it won't work. The "t" variable is not directly in scope here -- there is no "t" variable as part of your test class, or as a local variable of the main() method. So, "t.isAlive()" won't work here.

Henry
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Campbell,

Could you please tell me how to use what you have said? What do you mean by "t has a method"? 't' being a field, can have a method? I am lost!
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Henry,

Yes, I understand how t.isAlive() won't work here, but I am not able to comprehend ob1.t.isAlive() as well unfortunately
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
There might be a field called t, but it points to an object, and all objects have methods. Some only have these eleven methods, but most have more methods. It just so happens that the object pointed to by t has a method called isAlive().

But you already know that because t points to Thread objects (or null).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you had better design and made t private, only accessible via a get method, you could writeOr you can run it all together in one line like thisBoth would probably print true.
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Could you please give me an example for how a different construct could be used here?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Prasanna Raman wrote:Could you please give me an example for how a different construct could be used here?


You seem to be under the impression that this is some magical construct -- that is different from other reference variable types... However, given a reference variable to an object, you can access fields like so "var.field" and you can call methods like so "var.method()". But just what is a field? It is just a variable -- including a reference variable. What is returned from a method? It can possibly return a reference to an object.

How is this field, or reference return from a method different from any other variable? Well, it isn't much different. You can use the field as a variable to access the field of the object that the field variable points to ... like "var.field.anotherfield".

Henry
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Thank you very much, Henry! You hit the nail on the head.. I did think this was some magical construct. My sense was it wasn't but since I couldn't understand it, it seemed magical.

"But just what is a field? It is just a variable -- including a reference variable. What is returned from a method? It can possibly return a reference to an object.

How is this field, or reference return from a method different from any other variable? Well, it isn't much different."

This explanation from you has done the trick for me! I think I now understand this. Please confirm if my understanding is right,

"t" is just a field in the New class, just like "i" being a field if there was an "int i" declaration in the same class.

So, "t" is a field, but just that it happens to be an object of some other class thereby having the ability to access methods of that class to which it belongs.

So, "ob1.t" is just accessing the "t" field of New class, and with "t" being an object, it's possible to call a method on that object by "ob1.t.isAlive()".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Prasanna Raman wrote: . . . So, "ob1.t" is just accessing the "t" field of New class, and with "t" being an object, it's possible to call a method on that object by "ob1.t.isAlive()".
Yes. Got it! Spot on. Well done.
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
Thank you very much, Campbell and Henry

Campbell, I hope I made the code a little more readable and indented, thanks to you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: isAlive()