aspose file tools*
The moose likes Beginning Java and the fly likes Questions on a segment of java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Questions on a segment of java code" Watch "Questions on a segment of java code" New topic
Author

Questions on a segment of java code

cake naiyou
Greenhorn

Joined: Apr 29, 2011
Posts: 27
For the following segment of java code, the method of “run” occurs four times. I am quite confusing about the relationships of these four occurrences of “run”. Can you explain this to me? The original code is pretty long, I just keep the part that is related to my question.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Let's first look at the two methods in the class, lines 9 and 13. These are two methods both called run but with different arguments and a different return type. Note that in line 10, the first run method calls the other one (from line 13). In line 15, this method calls a run method in a completely different class (ClusterDriver) that you didn't show.

In the main method, on line 5, the run method of line 13 is called on a new Job object.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
cake naiyou
Greenhorn

Joined: Apr 29, 2011
Posts: 27
Thanks for the reply. Can I understnd the invoking sequence of this segment of code as follows:

At first, he run method at line 5 is called. Due to its particular parameter setting, 3 parameters, the compiler automatically uses the run method defined in line 13. ( if we only have one parameter in line 5, then compiler will use the run method defined in line 9 instead.

For the run method defined in line 9, it will call run method at line 10, which essentially is the run method defined at line 13.


Is my understanding correct?

Jesper de Jong wrote:Let's first look at the two methods in the class, lines 9 and 13. These are two methods both called run but with different arguments and a different return type. Note that in line 10, the first run method calls the other one (from line 13). In line 15, this method calls a run method in a completely different class (ClusterDriver) that you didn't show.

In the main method, on line 5, the run method of line 13 is called on a new Job object.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Yes.

If there are two methods with the same name in the same class, Java will look at the number and types of the arguments to decide which one to call.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions on a segment of java code