• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

About Methods?

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic