Ways to call a method

E

Ericsson

Hi
I am reading a book about Java . It says that "There are three ways to
call a method

1 - Using a method name by itself to call another method of the same
classsuch as maximum( number1, number2, number3 ) in line 21
2 - ........
3 - ........

I can not understand number 1

// Fig. 6.3: MaximumFinder.java
// Programmer-declared method maximum.
import java.util.Scanner;

public class MaximumFinder
{
// obtain three floating-point values and determine maximum value
public void determineMaximum()
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );

// prompt for and input three floating-point values
System.out.print(
"Enter three floating-point values separated by spaces: " );
double number1 = input.nextDouble(); // read first double
double number2 = input.nextDouble(); // read second double
double number3 = input.nextDouble(); // read third double

// determine the maximum value
double result = maximum( number1, number2, number3 );

// display maximum value
System.out.println( "Maximum is: " + result );
} // end method determineMaximum

// returns the maximum of its three double parameters
public double maximum( double x, double y, double z )
{
double maximumValue = x; // assume x is the largest to start

// determine whether y is greater than maximumValue
if ( y > maximumValue )
maximumValue = y;

// determine whether z is greater than maximumValue
if ( z > maximumValue )
maximumValue = z;

return maximumValue;
} // end method maximum
} // end class MaximumFinder
 
I

Ingo R. Homann

Hi,
Hi
I am reading a book about Java . It says that "There are three ways to
call a method

1 - Using a method name by itself to call another method of the same
classsuch as maximum( number1, number2, number3 ) in line 21
2 - ........
3 - ........

I can not understand number 1

Perhaps you are thinking too complicated. It is really trivial: If you
write "foo();", then the method "foo" is called. It just that easy.

BTW: I would be interested in number 2 and 3. One is 'reflection', I
suppose, and the other...?

(I hope, the book does not count "this.foo();",
"AnotherClass.this.foo();", and so on as different ways...)

Ciao,
Ingo
 
R

rajesh.raavipaati

dude
its just to understand the basics of classes and its method
properties
how to access to class elements -both data and methods in brief
we hav three type as protected public and private which defines the
accessability of class elements
generally other classes cannot access the private data expect by
methods of that class
case with public is complete different as they r defined to be for
public useage

in this example, we r calling a method of a class by a method with in
the class --just like intercourse between two family members
 
G

Gordon Beaton

in this example, we r calling a method of a class by a method with in
the class --just like intercourse between two family members

Some things are best declared "private"...

/gordon

--
 
A

Andrew Thompson


"Don't call me dude" And while we are at it, please
- find your 'shift key' and apply once at the start of each sentence.
- refrain from using SMS style abbreviations like 'r'
its just to understand the basics of classes and its method
properties ...

BTW - are you the OP? Or are you just stating that is
what you think?

...
in this example, we r calling a method of a class by a method with in
the class --just like intercourse between two family members

..I take it you mean *verbal* intercourse?
 
D

Daniel Pitts

Ingo said:
Hi,


Perhaps you are thinking too complicated. It is really trivial: If you
write "foo();", then the method "foo" is called. It just that easy.

BTW: I would be interested in number 2 and 3. One is 'reflection', I
suppose, and the other...?

(I hope, the book does not count "this.foo();",
"AnotherClass.this.foo();", and so on as different ways...)

Ciao,
Ingo
My guess is reflection isn't mentioned, and the two others are calling
it on an instance, and calling a static method.

myObj.foo();
MyClass.foo();
 
M

Mark Space

Ericsson said:
public class MaximumFinder
{
// obtain three floating-point values and determine maximum value
public void determineMaximum()
{
// determine the maximum value
double result = maximum( number1, number2, number3 );
} // end method determineMaximum

// returns the maximum of its three double parameters
public double maximum( double x, double y, double z )
{ }
} // end class MaximumFinder

As others have said, you are over reading this.

Look at the code above. I've simply snipped your example so the lines
are easier to find. See in the second bit where the comment says:
// determine the maximum value

That's what they mean. The method "maximum" is invoked on the line
right after that comment. It just calls the method "maximum" in the
same class. The line right after:
// returns the maximum of its three double parameters

That method right after than comment is invoked.


All it means is you can call methods from the same class with out using
the class name or the instance variable. The compiler will figure out
what you mean and do it. The call above is the same as calling:

double result = this.maximum( number1, number2, number3 );

"this" means "this instance variable" so you just call the a method
that's already in you class.


The only tricky bit is when you call a method that isn't declared
directly. For example, right after the call to maximum(), you could
have added:

String me = toString();

This calls your (instance of class MaximumFinder) toString() method.
Well you don't see one, but you have it, because you inherit toString()
from Object, which all classes subclass from. You'll run into methods
that a class inherits quite a bit. It's best to have a modern IDE so
you can quickly find where any method is declared. They can sometimes
come from unexpected places and won't be obvious like maximum() was.

Good luck.
 
S

Stefan Ram

Ericsson said:
1 - Using a method name by itself to call another method of the same
classsuch as maximum( number1, number2, number3 ) in line 21
I can not understand number 1

The restriction »another« is not needed as a method also
might call itself.

In Java, a method name is a special entity - not an expression
as in C++. An expression always can be enclosed in parentheses.
Therefore, in C++ the call

f(x)

also might be written as

(f)(x)

, because »f« is an expression in C++. In Java, »f« is a
»method name«, which is not an expression, so »(f)(x)« is not
a call in Java.

What is it that you can not understand about number 1?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top