18

I just decided to integrate my MATLAB programming skills with some more consistent and rigorous Java coding. Therefore I hope it's not gonna be a too naive question.

I'd like knowing if there is any real reason why Java refers to functions as methods and not as functions, as many other program languages do.

Is it because of the inner OOP Java's nature compared with procedural languages such as C/C++? or are there any other important (or subtle) reasons?

Thanks in advance.

9
  • 1
    methods are functions in classes with class scope... Commented Apr 25, 2013 at 19:52
  • 1
    I'm not real happy with the definition contained in the accepted answer in the "dupe", quite frankly; it still comes down to a matter of definition, and that definition is contextually dependent. Bottom line is "because that's what Java calls them". Commented Apr 25, 2013 at 19:54
  • @DaveNewton right spelling mistake tnks! Commented Apr 25, 2013 at 19:56
  • It's whatever you want to call it, if you're defining the language. Could be called a method, function, procedure, routine, and several others. Technically, a (mathematical) function has no side-effects, so the mathematically inclined shy away from that term (unless the routine is really required to not have side-effects). Commented Apr 25, 2013 at 19:57
  • function = static method (class wide method), without side effect. In contrast to non-static method which may alter fields of the object. Just because of the respect for the clean 'function' a new name, 'method,' was introduced. Commented May 2, 2013 at 9:43

4 Answers 4

8

Well there is a little difference between a method and a function.

A function is just a code that you can call anytime by its name and you can pass arguments also known as parameters to it and you can also get the result from any function i.e. return value of the function.

But a method is a code that is called by its name but it is associated to any object. You can pass parameters to methods also and you can also get some return value from methods but thing is they will always be associated with some objects.

EDITED

Java is object oriented, you cannot have Java code to run without classes in most cases however in C++ you can get your code run without classes. So in Java there will be classes and code will be written in classes so they are called methods instead of functions, as they will be associated with objects.

But in C++ you can have some function that can be called by passing values explicitly.

In simple terms you can say, a method is a function that is related to an object.

Sign up to request clarification or add additional context in comments.

6 Comments

Java is truly object oriented I wouldn't say so. Java has static methods (in fact the entry point to each Java program is a static method), and they belong to classes, but not to objects. Java is an imperative, procedural language that can (and should) be used in an Object Oriented way, but it's by no means a pure OO language
@SeanPatrickFloyd I didn't call it PURELY OO, I called it Truely. is it bad too ? and how can you say Java is procedural language ? Any Sources to read this thing ?
It's an OO language that can be mis-used in a non-OO way (nothing will stop you from writing a single main method thousands of lines long without a single object allocation). if that fits your definition of truely OO, our definitions differ
@SeanPatrickFloyd So in terms of OO what should we call Java? Partially?
Hmm, that would probably be too harsh. I'd reserve that for languages like Perl or JavaScript, where OO is basically an add-on. "Mostly"? or just "Object Oriented", without a modifier.
|
3

In my opinion this figure http://www.jot.fm/issues/issue_2008_03/article4/images/figure2.gif

one, two and three dimensional method dispatch

from http://www.jot.fm/issues/issue_2008_03/article4/ helps understanding one of the main differences between OO and procedural programming. Basically the idea is that

Procedural programming provides only one dimension to associate a computational unit with a name. Here, procedure calls or names are directly mapped to procedure implementations. In Figure a calling m1 leaves no choice but the invocation of the only implementation of procedure m1

while

Object-oriented programming adds another dimension for name resolution to that of procedural programming . In addition to the method or procedure name, message dispatch takes the message receiver into consideration when looking up a method. In Figure 2b we see two implementations of method m1. The selection of the appropriate method not only depends on the the message name m1, but also the receiver of the actual message, here Ry

the third section of the figure (c) refers to subject oriented programming, in which the behavior of an object (the called method) does not only depend on the object status but, also, on the subjects which is invoking (or observing) it. However this is actually out of the scope of your question.

Comments

1

Can't help thinking a lot of unnecessary drama in this one. "methods" is just a name surely, that Java happens to use, for subroutines which may or may not require parameters, and may or may not return a value?

E.g. valid "methods" might be as follows, without getting into OO purity, canonical definitions of "functions", etc; both of the below may or may not use an object's current "state" (instance variable values) in their execution too:

// kind of a function, returns a value public int calculateStuff(int param1) // more of a procedure, presumably just "does stuff", returns nothing public void doStuff(int param1) 

Comments

0

They're the same. C++ typically calls them functions. Java typically refers to them as methods.

Methods are typically associated with a class.

You'll occasionally hear "class function" too, which is just a method.

It doesn't matter, people will know what you're talking about if you call them either.

8 Comments

It's not true, even if we think it's the same thing. A method is for objects and a function is independent of objects.
You obviously have not been in the programming world long. Back before Java, we called methods "class functions". Don't get hung up on semantics.
Look at this post (Aaron answer). stackoverflow.com/questions/155609/…
I thought the real deal was functions return a value, procedures do not return a value, and method is just java speak for a function/procedure with an implicit parameter (this).
@CaptainSkyhawk I think it's reasonable to get hung up on semantics when discussing a question about semantics.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.