• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method calling  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This could be very basic question but studying threads I got a statement ,



I am little doubtful on this, Thread is class and getName() is method and currentThread() also is method. what is sequence in which these methods get executed and is there any rule that governs which method is called first.

Thanks in advance.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jatin Dhingra wrote:

Thread is class and getName() is method and currentThread() also is method. what is sequence in which these methods get executed and

currentThread() is the first method which gonna to executed first... then only the getName() method will execute....

sequence is first load the Thread class and using currentThread() getting the running thread, and using getName() get the name of the running thread...
Jatin Dhingra wrote:
is there any rule that governs which method is called first.

no idea... let us wait for result
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the documentation for java.lang.Thread class, you can find that currentThread() is a static method, which means it can be invoked without an instance by using the class name. And currentThread() returns a reference to the currently executing thread object. So you have a thread object after executing this- Thread.currentThread(). Now you can invoke the method getName() which is also part of Thread class.

Update:

Jatin Dhingra wrote: is there any rule that governs which method is called first.


To invoke a method- You need to have an object of the class to which the method belongs or just the Class (in case the method is Static)
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bharath,Mohamed.

In this particular statement, I can understand what you said. But consider a generalized statement like

object/class. First method. second method ;

How the compiler sees it ? I mean will it process left to right ?

As I see it, it first validates "object/class.first method". It sees then whether second method can be called on "thing" returned by this first method call.

please correct me if wrong.



 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can answer this yourself with a little example and follow-up question.

Now, which object is used for calling method3()?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write:

then that is exactly the same as:

It is processed from left to right. There is really no other way this could be done. Suppose it would be processed right to left, what would that mean?

The important thing to see is that object.methodOne() is an expression that produces a result of a specific type of object. On that type of object, you can call another method, for example methodTwo() if that type of object has a method with that name.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Maybe you can answer this yourself with a little example and follow-up question.

Now, which object is used for calling method3()?


Thanks Rob, Jesper.

@Rob: As per your comment and that of Jesper, Object returned by object.method1().method2() would have its method3 called. Is it right ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

One note though: this kind of method chaining makes it a bit harder to debug NullPointerExceptions. If you would get one on that line there could be a few causes:
- object is null
- object.method1() returns null
- object.method1().method2() returns null
- object.method1().method2().method3() returns null

So only use this kind of chaining if you know the method results will not be null. Thread.currentThread() will not return null so your example is safe.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You're welcome

One note though: this kind of method chaining makes it a bit harder to debug NullPointerExceptions. If you would get one on that line there could be a few causes:
- object is null
- object.method1() returns null
- object.method1().method2() returns null
- object.method1().method2().method3() returns null

So only use this kind of chaining if you know the method results will not be null. Thread.currentThread() will not return null so your example is safe.


Very right !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!