• Post Reply Bookmark Topic Watch Topic
  • New Topic

method().anothermethod() ???  RSS feed

 
Joel Ewest
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just finished a Java 101 class and am now attempting to use my new skills at work developing in Java(J2EE no less). I noticed in a lot of my companies code there are statements that look like this:

setAnObjectFactory(new AnObjectFactory(AnObject.getProperties().getString(servletName + ".BaseId")));

I am confused on how the two methods in the parm are connected. How does it work when you have "aObject.getSomething().getString()" ???

I didn't know you could connect to methods with the dot operator. How does this work?

Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel Ewest:
...How does it work when you have "aObject.getSomething().getString()" ???

I didn't know you could connect to methods with the dot operator. How does this work? ...

Welcome to JavaRanch!

Consider that the call aObject.getSomething() returns a reference to something. Then getString() is called on that reference. In other words, it's shorthand for...

Something s = aObject.getSomething();
String st = s.getString();

...without having to declare the variable "s."
[ October 26, 2006: Message edited by: marc weber ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that, despite its frequent use, this is often a bad idea. Many methods that return an object can also return null; if they do, then this kind of code can abort with a NullPointerException. Not pretty.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Note that, despite its frequent use, this is often a bad idea. Many methods that return an object can also return null; if they do, then this kind of code can abort with a NullPointerException. Not pretty.


Well, some people would say that returning null might be the problem here. Of course there are other problems related to method chaining, design-wise. But that's probably already too much for a beginners forum (interested people should google for the "law of demeter").
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


Well, some people would say that returning null might be the problem here. Of course there are other problems related to method chaining, design-wise. But that's probably already too much for a beginners forum (interested people should google for the "law of demeter").


Agreed on all points. But of course the Java APIs are full of method that can return null.
 
Joel Ewest
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah....I think I see how that works.

So if I return a reference to an object with a method I can then go ahead and use a method of the freshly returned object by using the dot operator. Does that makes sense?

Seems to me the code would be easier to read if they split it into multiple statements even if this does work. But I am a newbie so maybe I'll change my tune later.

Thanks for the answer!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel Ewest:

So if I return a reference to an object with a method I can then go ahead and use a method of the freshly returned object by using the dot operator. Does that makes sense?


That's exactly right.


Seems to me the code would be easier to read if they split it into multiple statements even if this does work. But I am a newbie so maybe I'll change my tune later.


You have several decidedly-not-newbies here, myself among them, arguing that this is a generally bad practice, for a number of reasons. So I'd prefer that you stick to your present opinion, if it's all the same to you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!