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

Problem with syntax regarding method call

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is From SCJP book of Bert Bates.

Although i understood the main concept behind this question - that flipper method is final but private in Clidder.So we can have same method in Clidlet also.
But i never came across something like:
new Clidlet().flipper();

The only time i know that we can use a class name to call a method is, when the method is static.So my question is
what does Clidlet().flipper do?
Of course by looking at it, it seems that it is calling flipper method from class Clidlet.
But why this syntax- Clidlet()

Here is the program:



What is the result?
A. Clidlet
B. Clidder
C. Clidder
Clidlet
D. Clidlet
Clidder
E.Compilation fails

The answer given in the book is option A which is as expected.But i don't understand the syntax.

And also Anytime i use an object , i use following syntax:
classname refvariable = new classname();

But in above program:
new Clidlet().flipper();
is used.

So how is this done?
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This tutorial might be helpful.

The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:

Point originOne = new Point(23, 94);

The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:

int height = new Rectangle().height;
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which means
is equivalent to
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Which means
is equivalent to


Ok thanks, i understood now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic