wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes direct/indirect References in Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "direct/indirect References in Objects" Watch "direct/indirect References in Objects" New topic
Author

direct/indirect References in Objects

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Where lies the difference between this:



and this:



In both cases, the mouse-property of name is set in Object Cat. Which one is better? differences?
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

what is there in both classes?


Dhruva
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Work this out with a pencil and paper: What is the Mouse object in the cat before and after each bit of code? Then you can see whether there is a significant difference or not. And by "w" did you mean "m"?

Some people like lots of invocations joined together by dots, some don't. It depends on what the methods return; if they return an object reference, you can use join-the-dots invocations, if they are void you can't.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
hello, yes I mean m instead of w.

In both cases, the Mouse-Object of Cat are changed.

This is obvious:



But this not:



So I guess, I prefer the direct way:



It s clearer.
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

nimo frey wrote:hello, yes I mean m instead of w.

In both cases, the Mouse-Object of Cat are changed.

This is obvious:



But this not:



So I guess, I prefer the direct way:



It s clearer.


it seems to me that
if we need value getting from Cat object,


if we need to insert new/different value than Cat object,


i think no change in performance if values to be set is equal as


am i rightly thinking?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Not sure; I think you need to try that code out in different contexts. I am not sure "direct" and "indirect" are quite the right words to use.

I think what you have here are "aliases": two names for the same object. You can say m.setName("Jerry") or c.getMouse().setName("Jerry").
You will have to try different combinations, and m.getName() or c.getMouse().getName(). I think you will find that m.setName("Jerry") followed by c.getMouse().getName() will return "Jerry".
Getting two different names for the same object can cause no end of confusion because you forget that calling a method on m also affects c.getMouse().

I think it is a case of what is easier to read, and I don't think there will be a performance difference.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Campbell Ritchie wrote:I think it is a case of what is easier to read, and I don't think there will be a performance difference.

+1
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

think it is a case of what is easier to read, and I don't think there will be a performance difference.


nimo asked for difference

for sure, is more readable than

but, is there any performance related difference if 'm' is used instead of


nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I think it is a case of what is easier to read, and I don't think there will be a performance difference.




and I don't think there will be a performance difference.


I hope so:-)
 
 
subject: direct/indirect References in Objects