Two Laptop Bag*
The moose likes Beginning Java and the fly likes What is a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a method?" Watch "What is a method?" New topic
Author

What is a method?

John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

So I've been programming in java for a little while, and I'm coming to a better understanding of the basics. I don't fully understand what a method is, or how it should be used. A definition and an example would be appreciated. I've oracles help doc, but would like a different explanation.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the beautiful world of Java language John. What do you think a method is? What is the meaning of the word "method" in plain English language as per the dictionary?


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8054
    
  22

John Corkrend wrote:So I've been programming in java for a little while, and I'm coming to a better understanding of the basics. I don't fully understand what a method is, or how it should be used. A definition and an example would be appreciated. I've oracles help doc, but would like a different explanation.

Why? What don't you understand about their definition?

Best I can come up with is that it's a named block of code - usually one that you're likely to execute many times - that is associated with a class (if it's defined as static) or an object (if not).

In the same way as you can supply a Maths function like log with an argument (the number you want to get the log of), you can also supply arguments (called parameters) to methods.

If you need more info, I suggest you read the tutorials, because there's quite a lot to know; and it'd be tough to cover it all in a forum like this.

Winston

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
John Corkrend wrote:So I've been programming in java for a little while, and I'm coming to a better understanding of the basics. I don't fully understand what a method is, or how it should be used. A definition and an example would be appreciated. I've oracles help doc, but would like a different explanation.

Why? What don't you understand about their definition?


And, when you found that definition wanting, and then you naturally did a Google search for something like "What is a Java method?", and you spent at least an hour studying those results, what in particular did you still have trouble with?
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Ok, well I understand it a little bit better now, it's basically code that can be run multiple times in a program. I guess what I didn't understand is how it is used in a program. Like what is an example of where I would use it in a program, also how do I call it in the program. I'm really just looking for some examples, thanks for the replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I would say that the fields of a class are an abstraction of data; the fields of an object actually hold the values of those data.
The methods of that class are an abstraction of some sort of behaviour (usually using those data) and calling those methods starts off that behaviour.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10491
    
    9

John Corkrend wrote: I'm really just looking for some examples, thanks for the replies.



[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
enombe thierry
Greenhorn

Joined: Apr 01, 2013
Posts: 10
To begin with i will like you to look at methods as a function.
A method is like a function that has a return type and takes a parameter(s).For examlpe look at this method dat i call sum,It is declared as such
public int sum(int x,int y)
this method is public cos it is available to the public(it can be called by any other class but if private it can only be used in the class it is been declared)
it returnns the sum of x and y which is and interger , So int means return interger at the end of the method
x and y are called parameters(no limits as to the number of parameters te be passed to a method) and int means that they are intergers(the type of parameter must be included)So
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to the Ranch

I have added code tags to your post; you should always use them, and you can see how much better the post looks
I would disagree that a method is a function. What you have quoted is a special case, which does constitute a function. It is also a type 1368 method in the most dubious classification known to modern science. In that case, it ought to be static.
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi John Corkrend,


when you are coding all the tasks are divided into modules.Every module can be considered as method.

for example:

void add()
{
}

void sub()
{
}

here you can write code seperatley in methods and can be reused
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is a method?