File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Struggling to understand methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Struggling to understand methods" Watch "Struggling to understand methods" New topic
Author

Struggling to understand methods

ray hancock
Greenhorn

Joined: Dec 31, 2012
Posts: 4
I basically understand that methods are simply a "method to do something" within a class. I'll explain specifically what I'm struggling with.

Using void and private ..don't fully understand this, and just methods in general.
for example..

String checkYourself(String userGuess)

So in my mind... This is a method that has been declared as a string assigned to checkYourself and will return the string userGuess.

So, is this right? How does this work are there some noob friendly examples out there? If this is right can someone use it in an example for me please.

I'm trying to see this in my mind and I think the objects rest at different virtual layers in the code. I'm stuck in my learning because I can't get past this concept.


Please and thank you guys!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

ray hancock wrote:
String checkYourself(String userGuess)

So in my mind... This is a method that has been declared as a string assigned to checkYourself and will return the string userGuess.


Nope. It's a package-protected method (because of no access modifier such as public) named checkYourself that returns a string value. It accepts one parameter ot type String that is accessible within the method by name userGuess.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

ray hancock wrote:String checkYourself(String userGuess)

So in my mind... This is a method that has been declared as a string assigned to checkYourself and will return the string userGuess.

Hi Ray,

Welcome to the Ranch! This is the perfect place to ask this type of question - there is no question and no programmer to 'noob' for these parts.

That synopsis of the method is not quite right. The way I would put it is:
checkYourself() takes a String userGuess, does something with it, and gives a String in response. It is hard to say more about what the method does to userGuess and what the return means without looking at some documentation (or code).

Methods are actions. The details inside the parenthesis are the inputs - the requirements to fulfill the action. The part to the left of the method name is the result of the action.


Steve
ray hancock
Greenhorn

Joined: Dec 31, 2012
Posts: 4
Thank you for your replies. I'm still struggling to understand, do you know of a youtube video where methods are explained in detail? Sometimes you learn differently if its taught to you instead of teaching yourself.
ray hancock
Greenhorn

Joined: Dec 31, 2012
Posts: 4
I think learning the syntax for java and learning the concepts of OO Programming are 2 different things. Maybe I need to focus more on understanding OO Programming?
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
ray hancock wrote:I think learning the syntax for java and learning the concepts of OO Programming are 2 different things. Maybe I need to focus more on understanding OO Programming?


Understanding methods really isn't an OO concept, you just have to learn how methods work in Java.

First of all, ignore what the access modifiers (public, private, etc.) mean for now. All you have to know for now is that public means your method can be accessed (called) from anywhere in the program.

Consider your method defined thusly:


The return type String after the access modifier simply means that calling the method will return a String (which does happen to be a fully fledged object).
With this knowledge we can substitute a call to the checkYourself method anywhere in the program where we could normally use a String. For example -


With this we are stating the variable iGuess will hold a reference to the String that is returned by our call to the checkYourself method.

Getting onto the rest of the method defintion, the bit in parenthesis is what we are stating must be given to method to work with when we call it.
We can state that the method should be given any number of things such as a primitive type or any object. We can also choose to state that the method
must be passed any number of things or nothing at all. For example, all of these would be valid:


We can think of the bit in between the parenthesis as the input to the method and the String specified after the access modifier as the output.

Within the body of the method we are required by the compiler to actually return the type that we specified the method as returning, by using the return keyword.
We are not required however, to actually use any of the input that is passed into the method.

When we run a program the main() method is automatically called for us as the starting point for the program execution and is always specified as returning nothing (void).

With all that in mind, can you guess what the output of this program will be?


I did have to add the static keyword, but the reasons why are beyond the scope of this post.
Hope that helps you get your understanding started.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

ray hancock wrote:I think learning the syntax for java and learning the concepts of OO Programming are 2 different things. Maybe I need to focus more on understanding OO Programming?

Like Steve, I don't think so.

As he said, methods describe actions, and all methods share the same basic structure. In the old days we used to use the mnemonic IPO, which is short for Input-Process-Output, where:
I = parameters,
P = code,
and
O = the return value
although they don't always have to have the I or O.
An example of a method that doesn't have parameters (the I) is toString(), and a method that doesn't return anything (eg, a "setter" method) specifies a return type of void.

If you think about it, it's rather like the way we do things ourselves: If you want to make a table, you will probably need wood, screws and glue (the input); you'll then need to plane it, saw it, and put all the pieces together (the process) to produce your table (the output). In Java terms, that might look something like:
HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Nice post, except for one tiny point:-
Steve Myers wrote: . . . The keyword String . . .
String isn’t a keyword. You can’t redefine a keyword, but you can redefine “String” to mean something different, which will confuse everybody in sight
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

My suggestion is to stop thinking too much about it. Take a look at how they are used - just about every book and tutorial on the planet is going to use them. Then start to use them yourself. It will fall into place pretty naturally once you get your hands on them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struggling to understand methods