---
title: "Java: where to use static methods"  
description: "Java: where to use static methods"  
author: "Allen Scott"  
published: 2015-09-04  
updated: 2015-09-04  
canonical: https://www.mindstick.com/forum/33439/java-where-to-use-static-methods  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Java: where to use static methods

I am wondering when to use [static methods](https://www.mindstick.com/forum/161339/static-methods-in-python)? Say If i have a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) with a few [getters and setters](https://www.mindstick.com/forum/160071/what-are-getters-and-setters-in-javascript-classes-and-why-are-they-useful), a method or two, and i want those methods only to be invokable on an [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) object of the class. Does this [mean](https://yourviews.mindstick.com/view/80768/what-does-real-minority-mean) i should use a [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp)?\
[e.g](https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom)\

```
Obj x = new Obj();x.someMethod
```

or\

```
Obj.someMethod(is this the static way?)
```

\
I'm rather confused

## Replies

### Reply by Anonymous User

First, ask yourself "does it make sense to call this [method](https://www.mindstick.com/forum/166/webservice-method), even if no Obj has been constructed yet?" If so, it should definitely be [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods).\
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 void setMileage(double mpg) (which sets the efficiency of one particular Car) can't be static since it's inconceivable to call the method before any Car has been constructed.\
(Btw, 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](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best), though.)\


---

Original Source: https://www.mindstick.com/forum/33439/java-where-to-use-static-methods

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
