1

I have seen that sometimes methods in a class that do not depend on the instance state are marked as static. I find that it makes it quite clear in that it advertises that 'this method is independent of instance state'. It is easy to skip that method entirely when reviewing for chances of inconsistent state.

But, the problem with marking the method static is that static methods can't be inherited or overridden.

Something like this is discussed here: Does it make sense to have a non static method which does not use an instance variable?

Would it make sense to have an annotation to declare that a non-static method is state-independent? Then the compiler could verify that it uses only other state-independent methods.

Is there any such feature in the pipeline?

Community
  • 1
  • 1
Teddy
  • 4,009
  • 2
  • 33
  • 55
  • 1
    Sure would. That is why C++ has *const*. Maybe you want to check to see if somebody put in a request in the java community process; if not; start it ;-) ... but seriously - the other view on *static* is: it is an abnormality within OO. Besides the problems you mentioned, static calls also lead to tighter coupling; and code that is harder to test. So the point is: you avoid it where possible. – GhostCat Jul 30 '16 at 09:15
  • 1
    @GhostCat Note that this is different from C++ const, this basically proposes overrideable/virtual static method. – hyde Jul 30 '16 at 09:17
  • @hyde Yes... When I pass two parameters to a method and get one return value, sometimes I want to be sure that (1) the method is using only these two inputs from this class and (2) it doesn't mutate the state in any way. – Teddy Jul 30 '16 at 09:30

1 Answers1

1

There is no such mechanism in java. Even static does not cover all cases. Static methods still can change state in static context.

class Example {
    static int counter = 0;
    static int add(int a, int b) {
         counter++;
         return a + b;
    }
}

But there is some research to solve this problem using static analysis. Looks like JPure has a working prototype

Nazarii Bardiuk
  • 4,272
  • 1
  • 19
  • 22
  • Thanks a lot. That's almost exactly what I was looking for. I wonder why its not a core Java language feature. I hope it gets built into the language at some point. By the way, would you have some suggestion regarding this: http://stackoverflow.com/questions/23995683/improvement-of-package-private-classes-in-java – Teddy Jul 30 '16 at 14:42
  • Yes it would be useful feature. Martin Odersky mentioned in his presentation about scala road map that in some point in future it will have effect system that is going to encode side effect in function signature. But I don't expect for such change in java at all. – Nazarii Bardiuk Jul 30 '16 at 15:39
  • I've been putting off looking at Scala for so long. I think I should try it out. I just love it when you don't have to look at a section of your code, because it is marked safe. Same reason we prefer to use Immutable collections, or mark fields final etc. Thanks. – Teddy Jul 30 '16 at 16:01