• Post Reply Bookmark Topic Watch Topic
  • New Topic

why a class name shows in front of a method name ?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I am very new to JAVA. I am reading Thinking in JAVA these days. Hope to build some basic concepts of it. When I read this code it really confused me. As I understood the increment() is a method and it is in class Leaf. But in this case the code is Leaf increment() {...} and why did the complier put a class name in front of the method name? I tried another way, which is use a int instead of the Leaf, then instead of using 'return this; ' use return i++ directly. At last change the 'x.increment().increment().increment().print();' to four separate sentence. Then the code works properly. But how to understand Leaf increment() ? Thank you.

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mara. Welcome to the Ranch!

That's just the return type. All methods have a declared return type (though it can be void if you don't want to return anything). In that particular case the method is declared to return a Leaf object. To some extent it's coincidental that this is the same type as the class it's on.

As it happens, it's returning the same Leaf object that the method was called on (that's what return this means). That's a technique that then lets you use the shorthand format your line 15 has. It could have been written:
But, because increment returns x (in this case), this is exactly equivalent to:
Or it could even have been
It's a matter of personal preference which style you prefer.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mara sam wrote:I understood the increment() is a method and it is in class Leaf. But in this case the code is Leaf increment() {...} and why did the complier put a class name in front of the method name?

First: the compiler did nothing. You did.

But, to answer your question: the Leaf is what the method returns. You notice that the method contains a 'return this' statement, and that is what returns the Leaf object. If it didn't, you wouldn't be able to use 'increment().increment()', because the 2nd increment() would have nothing to work with.

HIH

Winston
 
mara sam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew. Thank you so much for your explain! it make so much sense


Matthew Brown wrote:Hi Mara. Welcome to the Ranch!

That's just the return type. All methods have a declared return type (though it can be void if you don't want to return anything). In that particular case the method is declared to return a Leaf object. To some extent it's coincidental that this is the same type as the class it's on.

As it happens, it's returning the same Leaf object that the method was called on (that's what return this means). That's a technique that then lets you use the shorthand format your line 15 has. It could have been written:
But, because increment returns x (in this case), this is exactly equivalent to:
Or it could even have been
It's a matter of personal preference which style you prefer.
 
mara sam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
mara sam wrote:I understood the increment() is a method and it is in class Leaf. But in this case the code is Leaf increment() {...} and why did the complier put a class name in front of the method name?

First: the compiler did nothing. You did.

But, to answer your question: the Leaf is what the method returns. You notice that the method contains a 'return this' statement, and that is what returns the Leaf object. If it didn't, you wouldn't be able to use 'increment().increment()', because the 2nd increment() would have nothing to work with.

HIH

Winston


Hi Winston. Thank you for your explain! It really helps
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Likewise.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!