| Author |
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
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
Joined: Mar 10, 2008
Posts: 1405
|
|
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
Bartender
Joined: Apr 06, 2010
Posts: 3791
|
|
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.
|
 |
 |
|
|
subject: Problem with syntax regarding method call
|
|
|