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...?
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.
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