File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new class.method()

 
Kok Lum Chan
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,



The above codes are from K&B SCJP6 (Chapter 2).

I have a difficulty understanding the last line. Usually, we do something like below :

why oWhy = new why();
oWhy.flipper();

Why this time it can allocate an object to the heap and calling the method at the same time ?

Any advice is appreciated.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
update: fixed
 
Relja Damnjanovic
Greenhorn
Posts: 6
MySQL Database Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi koklum!

The thing you're asking is pretty simple - in the case of the written code the author doesn't need the object instance any more so he/she is using the shorter syntax for the call.

So, in your example you create the object and reference it with a variable oWhy



and then invoke the method on that object



In the book example case, the created object is not assigned to variable, but is directly used only to invoke the method.



After this line this object is unreachable so if we would like to use it again we would have to instantiate it again.

Take look at the chapter dealing with Strings and its methods, you should see more examples like this dealing withe the piping the methods one to another, basically it's a same thing - only here first called method is a constructor, and constructors have different syntax for invocation.


 
Kok Lum Chan
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Relja,

Thank your for your clear explanation. It make sense to me now.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic