This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with syntax regarding method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with syntax regarding method call" Watch "Problem with syntax regarding method call" New topic

Problem with syntax regarding method call

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
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
D. Clidlet
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

Joined: Mar 10, 2008
Posts: 1914

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;

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4537

Which means
is equivalent to
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Matthew Brown wrote:Which means
is equivalent to

Ok thanks, i understood now.
I agree. Here's the link:
subject: Problem with syntax regarding method call
It's not a secret anymore!