15

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.

fpe
  • 2,700
  • 2
  • 23
  • 47
  • 1
    methods are functions in classes with class scope... – Anthony Raimondo Apr 25 '13 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". – Dave Newton Apr 25 '13 at 19:54
  • @DaveNewton right spelling mistake tnks! – Anthony Raimondo Apr 25 '13 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). – Hot Licks Apr 25 '13 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. – Joop Eggen May 02 '13 at 09:43
  • This is a very very fine point of semantics. Focus on what you need to do and not on minutiae of terminology. Everyone in Javaland will understand when you say "a class has a function" – millimoose May 02 '13 at 10:12
  • @JoopEggen That's just conflating so many things. A static method can't change the state of an instance because it's not associated with one. It can still change the state of any of its parameters if they're mutable. An instance method needn't necessarily change the state of the object it's invoked on, it merely has the possibility. This falls apart even further if you consider OO systems with multiple-dispatch, where the method may be considered to be invoked on all its parameters. The concepts of "immutability" and "being bound to an object" are unrelated. – millimoose May 02 '13 at 10:39
  • My personal distinction would be that a method is a function whose implementation *may be* bound at runtime based on the type of one or more of its parameters. (Where `this` is considered a parameter.) Of course this doesn't square with how Java uses the term, because of "static methods". The accepted answer on the dupe focuses on access to `private` members (i.e. data hiding), which I don't think is nearly as important in the context of OO as this (i.e. polymorphism) - insofar as many languages that call themselves OO exist that don't enforce access levels in the language. – millimoose May 02 '13 at 10:43
  • @millimoose, I just wanted to throw in a one-liner, but good to see a detailed treatment. – Joop Eggen May 02 '13 at 10:55

4 Answers4

6

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
  • 4
    *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 – Sean Patrick Floyd May 02 '13 at 09:55
  • @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 ? – gprathour May 02 '13 at 10:02
  • 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 – Sean Patrick Floyd May 02 '13 at 10:13
  • @SeanPatrickFloyd So in terms of OO what should we call Java? Partially? – gprathour May 02 '13 at 10:25
  • 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. – Sean Patrick Floyd May 02 '13 at 10:35
  • @GPS The distinction between "purely" and "truly" (or rather, the definition of "truly OO") is something that doesn't seem to exist outside your head, and thus isn't really a useful descriptor in an answer. Try to avoid making up terms unless it's to avoid repeating yourself. – millimoose May 02 '13 at 10:41
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.

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
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)
Brian
  • 6,391
  • 3
  • 33
  • 49
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.

Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39