0

According to Are there guidelines on how many parameters a function should accept?, it is ok for a method to have few parameters.

According to https://stackoverflow.com/questions/11240178/what-is-the-gain-from-declaring-a-method-as-static/11240227#11240227, if a non-static method doesn't rely on any non-static members, it is better be static.

How about the case that a method just use a few non-static members? For example:

public class MyClass{
    private String userId;
    private String name;
    .
    .
    .
    public void callSomeAPI(){
        .
        .
        .
        String url=...+this.userId+...;
        //other code

    }
}

callSomeAPI() doesn't need to override, but it can't be static currently just because it needs

this.userId 

only. So my question is, is it worth to convert the non-static members into parameters, i.e.:

public void callSomeAPI(String userId){
    .
    .
    .
    String url=...+userId+...;
    //other code
}

if it needs very few non-static members only?

ocomfd
  • 5,652
  • 8
  • 29
  • 37

2 Answers2

1

TL;DR: yes, but for private methods.

As your second link says, keep the scope of any variables as narrow as practicable. So if you can restrict a method to only using parameters, rather than fields, then do so... with the caveat detailed by your first quote that the number of parameters should be kept as small as possible to avoid calls to that method becoming unwieldy.

So it’s a trade off as these things often are. If the method only has a need to access one or two fields, I’d go for making it static and would pass them in as parameters. If it needs access to more, or if it needs to modify those fields, keep it as an instance method.

However, to re-iterate my first statement, such a transformation into a static method makes sense for private methods. If you did this to a public method, then you further need to consider the roll those fields play in you class.

  1. If they are simply passed in via the constructor, so that they are available to your instance methods, then the whole class becomes a candidate for becoming static (as long as the methods don't have global side effects).
  2. If they have state created or mutated by the instance, then they most likely make sense to remain as instance methods. Otherwise you risk ending up with global state, which is never a good place to be, or you’d need to expose those fields publicly too, ruining encapsulation.
David Arno
  • 38,972
  • 9
  • 88
  • 121
  • Converting an instance field into a parameter to a `static` method is not an equivalent proposition. Parameters passed into `static` methods are not persisted on a per-instance basis. – Robert Harvey Apr 26 '18 at 05:34
  • @RobertHarvey if the field needs modifying then as I said, static methods aren’t suitable. If that’s not what you mean, then I’m unclear what “... are not persisted on a per-instance” basis refers to. – David Arno Apr 26 '18 at 05:59
  • @RobertHarvey, I updated my answer as I realised it was misleading. I took as read that the OP was talking about private methods, but this may not be the case. So I updated my answer to make it clear that my advice only applies to such methods; not public ones. – David Arno Apr 26 '18 at 08:27
0

Absolutely no. getUserInfo returns a property of that object, that's why it is an instance method. Turning it into a static method with the name passed in is completely arse backward. And user.id is a private method, so you can only call this from methods inside the class. Don't do this.

gnasher729
  • 42,090
  • 4
  • 59
  • 119