• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

A question about calling methods

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw the following code in an example I was looking at:

I dont often see a call to a method like class.somemethod.somemethod. What is this saying in plain english? What cases would you use this in? Is it just for a Process object? Would you ever see class.somemethod.somemethod.somemethod...?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The methods resolve themselve from left to right.
So, Runtime.getRuntime() returns an "anonymous" Runtime object (because it has no variable name). Then the exec() method is called using that "anonymous" variable. (Kind of like "anonymous".exec(...) )
This is a trick that C++ people used to use a lot (they might still, I just haven't seen it much). By defining most methods in a C++ class to return "this", methods could be chained.
I treat this trick like the terniary if: In small doses it is very convenient, but it gets out of hand quickly. In your example, it is a quick and dirty (and relatively readable) way to do something without creating extraneous variables.
Bruce.
 
Sheriff
Posts: 16936
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just short cut for:
<pre>
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe");
</pre>
You'll probably see this type of shortcut used a lot, just needs a little getting use to.
Yes, you could possibly see a chain of the form class.method.method.method.method...
If the chain gets too long though, it is sometimes clearer just to break it up into smaller, more understandable pieces. Personally, I would have a few choice words for programmers who write long chains like that
J.Lacar

 
What are you doing? You are supposed to be reading this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic