aspose file tools*
The moose likes Beginning Java and the fly likes About Methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Methods?" Watch "About Methods?" New topic
Author

About Methods?

Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
What exactly are methods?

I hear that within each class, there is only one syntax to call any method present in that class? is that so?

could you give an example? for example how to invoke methods in the Math Class?


Ali
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
A "method" in Java corresponds to a "function" in C, a "sub" in Perl, a "procedure" or "function" in Pascal, etc. However Every method in Java has to be in a class. This is in contrast to languages like C++ where classes have their own methods/functions but you can have "free-floating" functions that aren't part of a class. This certainly isn't a rigorous definition but should be enough to give you the right idea. If you need more detail, just say so.

Some Java methods can be called with just the class name but without needing to have an object of that class instantiated. The Math class methods are good examples of these "static" methods.

Is there only one way to call a method in a class? If you're talking about methods in the same class, then you can prefix the method call with "this" but you don't have to. If the method is static, you can either use the class name as a prefix or you can use an object to designate which method to call.


Lines 8 and 9 do the same thing, and lines 11, 12 and 13 do the same thing.

Also, you have your choice of importing any classes that aren't in the same package as the current class or you can specify the package inline. For instance, Sorter1 and Sorter2 below do that same thing.


If you want to a bit more advanced, you can use the classes in the java.lang.reflect package to call the methods in a class, but that doesn't fit under "beginner".

As for the Math class, you're not allowed to make a Math object, so you're "stuck" using the syntax like line 12 in the first hunk of code. (...unless you're using Java 1.5, in which case you should Google "java static import".)


Ryan
[ June 24, 2005: Message edited by: Ryan McGuire ]
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
You might say, that methods are an abstraction for "behaiviour". In other
words it's the operations or functions the particular object can perform.

The syntax is: <object-reference>.<method-name>

The Math class has a method (as far as I recall) called "max" which
gets two integers, and returns the biggest numner. You'll call that
method with: Math.max(10,20); remember, however, that this
particular method returns a value.

The following code show's a few method calls on the Person object
called svend. If you feel like it, copy the code for the two classes
in two separate files, try to expand the Person class' behaiviour
(e.g. a getsYounger method), and compile and run it.



/Svend Rost
 
Don't get me started about those stupid light bulbs.
 
subject: About Methods?