aspose file tools*
The moose likes Cattle Drive and the fly likes methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "methods" Watch "methods" New topic
Author

methods

robert woolley
Greenhorn

Joined: Sep 04, 2003
Posts: 5
I'm having trouble understanding methods, what they look like, what they do and how they are used correctly. I have read these two course manuals that i have but because of the amount of information, I'm getting totally blown away. Can anyone help me with shown examples and highlight the methods in each case, that would be great if you could.
Thanks in advance.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
This page which is referenced from Assignment 4 - Say may help.

You are asking a very general question.



main is a method that takes a String array named "args" as a parameter. "public" and "static" are modifiers of the method and "void" is what the method returns (in this case, nothing). What the "main" method does is enclosed within the curly braces. In this case it does nothing. It is an empty method.



Now the main method is doing something. It is printing "in main" so you can see it on the monitor.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
It's easy, methods do anything you want them to!!!
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Oh Robert, you are experiencing something very similar to what I experienced when I first started the cattle drive. Trying to assimilate all of the terminology and theory all at once will drive you crazy. Take it in small pieces, and remember that sometimes if you walk away from the problem for a while, an idea will come to you.
Now for methods........
As I recall, trying to figure out what a method was and when to use one was a bit difficult for me, too. To put it as simply as possible, a method is a series of commands enclosed in curly braces {} and given a special name. When that name is called, the commands inside the braces are executed. It's that simple. Methods can do something, like print out a message and not return any value, or they can return something, like performing calculations and returning a result. It all depends on what you need your program to accomplish.
If you see repetition in the commands in your code, then it's probable that you could write one method that could be used over and over again in place of that repetition. To my mind, it's like distilling your code down to it's purest form. No needless repetition. Elegant.
My advice is to write your code, submit it for nitpicking, and you'll learn all you want to know about methods. And probably more than you wanted know......
robert woolley
Greenhorn

Joined: Sep 04, 2003
Posts: 5
So you could say that methods are kind of like the procedures/functions of a logic based programming language. So if i want to have a message pop up when centain information is entered incorrectly over several different classes, i could wrap them into one class called message and create one method, it would then work throughout all classes when called.
I will nail this i just need to get down to the practical side of things ASAP.
Thanks.

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Don't let the fact that Java is considered an Object Oriented language fool you into thinking that it doesn't eventually break down to procedures. If you really try hard enough, you can force Java to be procedural and write everything in one big main method (which I've seen done... by an instructor... in grad school... :roll: ).
Within a method, processes go step by step, line by line. If method A calls method B, then processing jumps to method B, and steps through the lines in that method. When processing is done, it goes back to method A and continues.
So yes, you could have something like:

If you have some method that finds an error you can call Message.printMessage() and "This is an error message!" will be printed. Your program will then continue normally (or not, depending on what you want to do after the error is found!).
Mind you, this isn't the way I would normally handle such a thing, but for simplicity sake we'll go with it
[ September 10, 2003: Message edited by: jason adam ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods