One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.

So in a class Car you might have a method:

double convertMpgToKpl(double mpg)

...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):

void setMileage(double mpg)

...can't be static since it's inconceivable to call the method before any Car has been constructed.

(By the way, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g.:

Car theMoreEfficientOf(Car c1, Car c2)

Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.

Answer from not-just-yeti on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ static-method-in-java-with-examples
Static Method in Java With Examples - GeeksforGeeks
A static method in Java is associated with the class, not with any object or instance.
Published ย  4 days ago
Top answer
1 of 16
1671

One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.

So in a class Car you might have a method:

double convertMpgToKpl(double mpg)

...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):

void setMileage(double mpg)

...can't be static since it's inconceivable to call the method before any Car has been constructed.

(By the way, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g.:

Car theMoreEfficientOf(Car c1, Car c2)

Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.

2 of 16
614

Define static methods in the following scenarios only:

  1. If you are writing utility classes and they are not supposed to be changed.
  2. If the method is not using any instance variable.
  3. If any operation is not dependent on instance creation.
  4. If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
  5. If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
Discussions

object oriented - Why have private static methods? - Software Engineering Stack Exchange
That can help other developers ... treat the method. Is it necessary, no - like so much, but it can be helpful information. ... A fairly common reason (in Java) would be for initializing immutable field variables in a constructor by using a simple private static method to reduce ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
April 1, 2014
What is the static method? - Oracle Forums
I have been working with Java Software Solutions and , I came to methods. But, i have not understand static method concept. What is this? Why we need to use this type of method? Why is main method sta... More on forums.oracle.com
๐ŸŒ forums.oracle.com
February 18, 2007
Can a static method be overridden in Java?
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 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. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar 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: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) 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: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } 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. More on reddit.com
๐ŸŒ r/javahelp
15
1
October 22, 2024
Can a static method be overridden in Java?
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://i.imgur.com/EJ7tqek.png ) 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: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } 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. More on reddit.com
๐ŸŒ r/learnjava
11
11
October 19, 2024
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ static method definition in java
r/learnjava on Reddit: Static method definition in Java
April 13, 2022 -

So I searched up the definition of static

โ€œIn Java, a static method is a method that belongs to a class rather than an instance of a class. The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that object of a class.โ€ -Techopedia This definition almost gave me a stroke ๐Ÿ’€

I was wondering if it just means that creating a static method (just attaching the keyword static to a method declaration) means I can just use it without creating an object? And so when I want to use the method I donโ€™t have to write like Object.methodName() I can just call it/use it directly like methodName()??? Similar to how weโ€™ve been calling and using methods in procedural style programming where you just have the static main method and then was able to casually call any other methods youโ€™ve created down below?

Is that all there is the static keyword? A method you can use without having to create an object and not needing to write Object.methodName() and just call it directly like methodName()???

Iโ€™ve trying to wrap my head and revisit this term.

๐ŸŒ
CodingNomads
codingnomads.com โ€บ how-to-call-static-nonstatic-methods-in-java
How to Call Static & Non-Static Methods in Java?
This article breaks down how to invoke (aka "call") static and non-static methods within the same class as well as external classes.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ what is static method in java with examples
What is Static Method in Java with Examples
September 12, 2024 - ... In Java, a static method is a method that belongs to the class, rather than an instance of the class. Static methods are accessed through the class name, rather than an object of the class.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csjava โ€บ Unit6-Writing-Classes โ€บ topic-6-7-static-vars-methods.html
6.7. Static Variables and Methods โ€” CS Java
Static methods only have access to other static variables and static methods (unless the static method creates an object through which to access instance variables and methods). Static methods cannot access or change the values of instance variables or use the โ€œthisโ€ reference (since there is no calling object for them), and static methods cannot call non-static methods.
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ use cases for static methods in java
Use Cases for Static Methods in Java | Baeldung
January 8, 2024 - Static methods are common to most object-oriented programming languages, including Java. What differentiates static from instance methods is that they have no object that owns them.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java โ€บ static method in java with examples
Static Method in Java With Examples - Scaler Topics
April 1, 2024 - The static methods are class methods that don't belong to instances of the class. They are designed to be shared among all instances of the same class and are accessed by the class name of the particular class.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 04 โ€บ java-static-class-block-methods-variables
Java โ€“ Static Class, Block, Methods and Variables
Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without object. Letโ€™s take an example to understand this: Here we have a static method myMethod(), we can call this method without any object because when we make a member static it becomes class level.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_keyword_static.asp
Java static Keyword
The static keyword is a non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class. Read more about modifiers in our Java Modifiers Tutorial.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ static-variables-in-java
Static Variables in Java โ€“ Why and How to Use Static Methods
March 7, 2023 - As you can see above, we declared ... static in Java programming, it means that the variable belongs to the class itself rather than to any specific instance of the class....
๐ŸŒ
Quora
quora.com โ€บ What-is-a-simple-explanation-of-a-static-method-in-Java
What is a simple explanation of a static method in Java? - Quora
Answer (1 of 8): Static methods: * When the behavior of a method is common for all the objects then we can make that method as static method, where common copy of behavior is maintained for all the objects.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ a guide to the static keyword in java
A Guide to the Static Keyword in Java | Baeldung
January 8, 2024 - In this tutorial, weโ€™ll explore the static keyword of the Java language in detail. The static keyword means that a member โ€“ like a field or method โ€“ belongs to the class itself, rather than to any specific instance of that class.
๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ what-is-the-static-method-0922
What is the static method? - Oracle Forums
February 18, 2007 - I have been working with Java Software Solutions and , I came to methods. But, i have not understand static method concept. What is this? Why we need to use this type of method? Why is main method sta...
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ static method in java | syntax, uses, limitations (+examples)
Static Method In Java | Syntax, Uses, Limitations (+Examples)
November 29, 2024 - A static method in Java is a method that belongs to the class rather than an instance, and can be called without creating an object of the class.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ java-static-method
Java Static Method | Static Keyword In Java | Edureka
March 1, 2023 - The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that member of a class. ... //Java Program to demonstrate the use of a static method.
๐ŸŒ
LaunchCode
education.launchcode.org โ€บ java-web-development โ€บ chapters โ€บ classes-part2 โ€บ instance-and-static-methods.html
5.2. Instance and Static Methods โ€” Java Web Development documentation
A method should be static when it does NOT refer to any instance fields of the containing class (it may refer to static fields, however). These methods tend to be utility-like (e.g. carrying out a calculation, or using or fetching some external resource). One common error new Java coders encounter ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ difference-between-static-and-non-static-method-in-java
Difference between static and non-static method in Java - GeeksforGeeks
June 30, 2023 - A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class.
๐ŸŒ
Pluralsight
pluralsight.com โ€บ tech insights & how-to guides โ€บ software development
Static vs Non-Static Java Overview | Pluralsight
May 19, 2025 - In Java, different elements can be static: Variables โ€“ A single shared variable for all instances of a class. Methods โ€“ Methods you can call without creating an object.