• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struggling to understand methods

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
ray hancock
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hold that thought. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic