r/learnjava • u/Virtual-Possible-233 • Apr 13 '22
What is the difference between a function and a method.
Itβs very confusing. Are they the same
49
u/hikerr7 Apr 13 '22
A function is independent of any object or any class. A method is on an object or is static in class.
You can create functions and methods in languages like C++ and python. In a procedural language like C you can create only functions. In java everything is an object so you can only create methods.
14
u/KronenR Apr 13 '22
This is wrong. A method is always a function, but not every function is a method.
A function is a block of code that performs a task that can be called and reused multiple times.
A method is just a function that is part of a class.14
u/makingthematrix Apr 13 '22
There are lambdas in Java, i.e. anonymous functions. They are instances of a special class but for all practical purposes they are functions.
1
u/Straitstan Apr 13 '22
Lambdas also exist in python and I think some other languages (not sure)
2
1
1
-4
13
u/LeeRyman Apr 13 '22 edited Apr 13 '22
It's more of a distinction in other languages, less so in Java because you don't have the concept of a function outside of a class.
If we talk generally, a method is a function that belongs to a class, either to an instance, or in the case of a Static Method, to the class definition. Ie, a chunk of code that has a symbol by which it can be invoked, but also has some automatic context - references to an instance and it's members and/or class members. A function in general does not necessarily have such context.
In C++ functions outside of classes are often called "free functions", to distinguish them from functions which are methods. In C you just have functions in the general sense, methods don't inherently exist as it is not an object oriented language.
Where I have heard it used, and probably used the term function myself, is for static methods in Java classes designed purely as containers of 'utility' code (final with private constructor so it can't be instantiated, and only static methods). It's a technique which is frowned upon in some circles though, YMMV.
Edit: I'm not saying that I dislike it, just don't be surprised to see code style guides that prohibit pure utility classes with only static methods. I've had to defend such code under review.
11
u/Migeil Apr 13 '22
It's a technique which is frowned upon in some circles though, YMMV
I don't get this to be honest. I've heard people say "I don't like static methods" and I don't really get why. If you don't need any context and just have a straightforward function, why would you go through the trouble of making it a non static class, having to instantiate it to gain access, in the process create an implicit parameter to the function which it doesn't use and allowing bugs to creep in because suddenly you have some sort of state in the object..
5
u/nutrecht Apr 13 '22
I don't get this to be honest.
It's not really a thing between professional devs. Static and non-static functions both have their uses. They're both just tools. "Disliking" either is just a strong indication someone is inexperienced and/or just repeating stuff they heard.
7
u/makingthematrix Apr 13 '22
One good reason is that if you have a method in a class, you can mock them in unit tests, that is, make a subclass and override the method with a trivial implementation which will just return the result you want. The code you're testing doesn't need to know that it calls a mock method instead of the real one. But if the code calls a static method directly, that method cannot be mocked.
More generally, keeping methods in classes let you replace their implementation. Static methods are, well, static in that respect.
But if you take a dive into functional programming, you will quickly learn that there are other ways to replace one function with another, and that mocks aren't always a good way to unit test.
3
u/LeeRyman Apr 13 '22
If I was to hazard a guess, it was possibly a philosophical decision to keep the language and it's class file format and linkage simple, and to encourage the whole OOP paradigm. Everything is contained within a well prescribed format with heaps of useful metadata, and I recall when learning it moons ago at uni, the focus was on modelling everything accurately as objects with the right level of abstraction.
2
u/KronenR Apr 13 '22 edited Apr 13 '22
What they say is that when you need static method classes it is because there is a flaw in your design.
And that flaw is that you lose one of the SOLID principles, Dependency inversion.
When you use static methods classes your code depends on those classes and if you want to substitute them for another implementation in the future (which you have no idea if you'll need or not unless you are clairvoyant) then you need to change all the references to that static methods classes in your codebase.0
u/nutrecht Apr 13 '22
It's a technique which is frowned upon in some circles though, YMMV.
That's really not a thing.
6
6
4
-1
u/my5cent Apr 13 '22
Perhaps it's js calling it that and java the later but no word police so ppl use it to mean the same.
1
β’
u/AutoModerator Apr 13 '22
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full - best also formatted as code block
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.