This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange code

 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stumbled uppon this code :



Is it valid code and what exactly does it do?
Thank you.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you find strange about it?

You can write it on one line:


It does the same as this:

As you know, you use the "." syntax to call a method on an object: variable name, dot, method name, parentheses, semi-colon. If the method returns something, you can assign the return value to a variable. Then on that variable you can again call a method.

You can also do this in one go. So, for example the expression new ClassName(this) gives you a ClassName object; you don't need to assign that to a variable first, you can directly call a method on it by putting a "." and the method name after it: new ClassName(this).setMethod1();. And the method call in turn can return an object, on which you can again call a method directly. So you can chain multiple method calls like that.
 
Nick Petas
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes of course.
Now I get it.
Thanks a lot for the rapid answer!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic