Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Ways to call a method

Thread view: 
Ericsson - 16 Nov 2007 01:32 GMT
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
Ingo R. Homann - 16 Nov 2007 08:26 GMT
Hi,

Ericsson schrieb:
> Hi
> I am reading a book about Java . It says that "There are three ways to
[quoted text clipped - 6 lines]
>
> 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
rajesh.raavipaati@gmail.com - 16 Nov 2007 09:28 GMT
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
Gordon Beaton - 16 Nov 2007 09:32 GMT
> 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

--
Andrew Thompson - 16 Nov 2007 09:44 GMT
>dude

"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?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Daniel Pitts - 16 Nov 2007 21:52 GMT
> Hi,
>
[quoted text clipped - 21 lines]
> 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();

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Mark Space - 17 Nov 2007 04:19 GMT
> 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.
Stefan Ram - 17 Nov 2007 04:33 GMT
>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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.