Hi, While passing parameters to methods, which style is preferable?? ClassType1 obj1 = new ClassType1(); RtnType1 value1 = obj1.someMethod(); ClassType2 obj2; RtnType2 value2 = obj2.anotherMethod(value1); OR RtnType2 value2 = obj2.anotherMethod( new ClassType1().someMethod()); I feel the second one is preferable. But I wish to know your opinion. Thanks Padmaja
hi, it depends if the object has a scope and serves a purpose for more then 1 method calls declare and use .. if not there is no purpose to declare and use it just use it as an anonymous object will be used .. for example even delegation where the method and object just comes alive for tht particular moment hope this helps ... cheers, parthi.
Even crazy and silly looking problems are sometimes real.
Hi, While passing parameters to methods, which style is preferable?? ClassType1 obj1 = new ClassType1(); RtnType1 value1 = obj1.someMethod(); ClassType2 obj2; RtnType2 value2 = obj2.anotherMethod(value1); OR RtnType2 value2 = obj2.anotherMethod( new ClassType1().someMethod()); I feel the second one is preferable. But I wish to know your opinion.
My vote goes to method #1, as it is much more readable. However, if the parameter were just a new object, I would do it as in method #2, because it is still clear, yet concise: RtnType2 value2 = obj2.anotherMethod( new ClassType1(someValue)); Eugene.
I agree with both Max and Eugene. Besides, Method #2 violates the Law of Demeter: Friends should only talk to friends. Michael Morris
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joined: Nov 14, 2001
Thanks guys, I thought #1 will have to allocate memory for the extra variables declared.
Friends should only talk to friends.
Michael, I like this explanation. Thanks Padmaja
Joined: May 15, 2002
Hi guys, yeah i agree it makes life easy to debug for example one might throw a null pointer error and u can just grab the line which throwed it in the case 1 but are'nt we making too many declarations ?? and there is no use for those dclarations after that line why cant we just create an anonymous object in the place of use ?? cheers, parthi.
town drunk ( and author)
Joined: Jun 27, 2002
This is actually a pet peeve of mine, so I'll add my two cents. in short, no, we are not. What you are creating is an extra reference, which is so cheap as to be insignificant. What does this mean? For example, say you have the following line 1: String tmp = "Hello"; line 2: System.out.println(tmp); vs line 3: System.out.println("Hello"); Line 3 create a new String variable, just as line 1 does. The only difference between the is creating of the reference 'tmp'. However, 'tmp' is not a new variable, it's just a reference. In effect, it's just a int, large enough to point to the memory location of the String "Hello". Thus, no real overhead, but a lot more clarity. HTH, M, author The Sun Certified Java Developer Exam with J2SE 1.4